Skip to content
YouTurkey Tech
All JavaScript topics

Promises & async/await

Intermediate

How a promise stands in for a result that isn't ready yet, when its callbacks run, and what await really pauses.Builds on Callback Hell, Event Loop

Call stack

Empty

    Web APIs

    Empty

      Microtask queue

      Empty

        Task queue

        Empty

          Event loop
          1. Running code
          2. Running microtasks
          3. Taking the next task
          4. Waiting
          Console

          Empty

          0/20Press Play, or step through with the arrows, to begin.
          Step 0 of 20

          Explanation

          2 min read

          On this page

          What is a promise in JavaScript?

          A promise is an object that stands in for a result that is not ready yet. It is in one of three states: (still waiting), fulfilled (it has a value) or rejected (it has an error). Once it settles, it never changes again.

          async/await is syntax on top of promises. An async function always returns a promise, and await pauses that function, and only that function, until a promise settles.

          How do promises and async/await run?

          • .then(fn) registers fn to run with the value once the promise is fulfilled; .catch(fn) runs on rejection; .finally(fn) runs either way.
          • .then returns a new promise, so steps chain flat, one after another, instead of nesting.
          • Promise callbacks and the code after an await run as microtasks: after the current code, before the next timer or event.
          • To run independent jobs at the same time, start them together and combine them: Promise.all (all must succeed), Promise.allSettled (wait for every result), Promise.race (first to settle) and Promise.any (first to succeed).

          What mistakes do people make with promises?

          Mini example

          async function showUser(id) {  try {    const response = await fetch(`/api/users/${id}`);    const user = await response.json();    console.log(user.name);  } catch (error) {    console.error('Could not load the user', error);  }} const [a, b] = await Promise.all([fetch('/api/a'), fetch('/api/b')]);

          Quick check

          Inside an async function, what does await pause?

          Further reading

          Watch the video

          The full explanation from the YouTurkey Tech YouTube channel, in Arabic.

          All steps as text
          1. Step 1:

            The script starts on the call stack. wait and brew are declared but not called yet.

          2. Step 2:

            start is printed.

            Line 12

          3. Step 3:

            brew() is called. An async function runs synchronously until its first await.

            Line 13

          4. Step 4:

            boiling water is printed.

            Line 6

          5. Step 5:

            wait(1000) is called and goes on top of the stack.

            Line 7

          6. Step 6:

            new Promise runs its function right away. It starts a timer that will call resolve in 1000 ms.

            Line 2

          7. Step 7:

            wait returns a promise that is still pending: a placeholder for a result that is not ready yet.

            Line 2

          8. Step 8:

            await meets a pending promise, so brew pauses and leaves the stack. It hands its caller a pending promise of its own.

            Line 7

          9. Step 9:

            .then registers a callback on brew's promise. Nothing is queued yet: it runs only once that promise is fulfilled.

            Line 13

          10. Step 10:

            end is printed. await paused only brew, never the rest of the script.

            Line 14

          11. Step 11:

            The script is done and the stack is empty. The loop waits for the timer.

          12. Step 12:

            After 1000 ms, resolve goes through the task queue and onto the stack.

            Line 2

          13. Step 13:

            Calling resolve fulfills the promise from wait. The rest of brew, after the await, is queued as a microtask.

            Lines 2–7

          14. Step 14:

            The stack is empty, so microtasks run: brew resumes exactly where it paused.

            Line 8

          15. Step 15:

            coffee ready is printed.

            Line 8

          16. Step 16:

            return 'coffee' fulfills brew's promise with 'coffee', so the .then callback is queued as a microtask.

            Lines 9–13

          17. Step 17:

            The loop is still emptying microtasks, so the .then callback runs at once, with drink set to 'coffee'.

            Line 13

          18. Step 18:

            enjoy coffee is printed.

            Line 13

          19. Step 19:

            The callback returns. The stack and both queues are empty.

            Line 13

          20. Step 20:

            Output: start, boiling water, end, coffee ready, enjoy coffee. No nesting: each step that waits is one flat line.