Skip to content

async / await and stack #74

Answered by pietro909
gunb0s asked this question in Q&A
Sep 29, 2023 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

I'm giving it a try to explain what I think occurs. I'm very happy if anyone can point out errors in my answer or improve it 🙏

The async/await syntax is syntactic sugar for Promises. Hence playingWithDelay can be rewritten as:

function playingWithDelay() {
    console.log('Delaying...', new Date())

    return delay(1000).then(dateAfterOneSecond => {
        console.log(dateAfterOneSecond)
        return delay(3000).then(dateAfterThreeSeconds => {
          console.log(dateAfterThreeSeconds)
          return "done";
         });
    })
}

Now it should be more clear how it works. You are registering a callback every time you use await.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@gunb0s
Comment options

Answer selected by gunb0s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants