async関数中にボタンを押すまで処理を止める例。
ほぼ使うときないけどメモ。
html
<button id="next-btn">next</button>
<script>
function waitClick(btn) {
return new Promise((resolve, reject) => {
btn.onclick = () => resolve()
btn.onerror = e => reject(e)
})
}
(async () => {
let n = 0
const nextButton = document.getElementById('next-btn')
while (n < 10) {
await waitClick(nextButton)
console.log(n++)
}
console.log('done')
})()
/** 出力
0
1
2
3
4
5
6
7
8
9
done
*/
</script>