0

Hello guys I am Erika a new developer. I am not very good with Javascript and I cannot chain these functions together. I read a few guides but somehow b executes before a even with awaits. Shouldn't the execution stop until a finishes?

async function a() {
    await setTimeout( async _=>{await console.log('a')}, 300)
}


async function b() {
    await console.log('b')
}


a().then(b)
console:
b
a
  • 2
    `setTimeout` does not return a promise, so `await`-ing it is not going to work as you think. Same thing with `await console.log`. What did you intend there?? – trincot Jul 14 '21 at 19:22
  • Using an `async` callback does not make `setTimeout` `await`able – Bergi Jul 14 '21 at 19:23
  • Thank you for your reply, I was trying to replicate the code that I have, which is some api calls, a() calls the api and gets the value that would be used in b(), so I want b() to execute after a() has finished, since they are both async. so b() ends up getting an undefined value from a(). – Erika Molidva Jul 14 '21 at 19:32
  • You want https://jsfiddle.net/v591kstd/ –  Jul 14 '21 at 19:34

0 Answers0