Agyan
adda
Run
Reset
Copy
JavaScript Tutorial › JS Async Await › Step by step with await
Back to tutorial
Code
Edit, then press Run or Ctrl+Enter
<button id="go">Place order</button> <pre id="out"></pre> <script> const wait = (ms, msg) => new Promise(r => setTimeout(() => r(msg), ms)); const out = document.getElementById('out'); async function placeOrder() { out.textContent = ''; try { out.textContent += await wait(700, '1. Order received') + '\n'; out.textContent += await wait(700, '2. Payment confirmed') + '\n'; out.textContent += await wait(700, '3. Packed and shipped') + '\n'; out.textContent += 'Done!'; } catch (err) { out.textContent += 'Something went wrong: ' + err; } } document.getElementById('go').onclick = placeOrder; </script>
Result