When trying to understand async/await, I tried this block of code that didn't behave the way I expected it to.
let test = async function () {
let result = new Promise((resolve, reject) => {
setTimeout(() => resolve('done1'), 2000)
})
let result2 = new Promise((resolve, reject) => {
setTimeout(() => resolve('done2'), 2500)
})
let x1 = await result
console.log(x1)
let x2 = await result2
console.log(x2)
}
test()
I was expecting the console to display done1 after 2seconds, then display done2 2.5 seconds after done1. Instead, It displayed done1 in 2seconds and done2 0.5seconds after that. Why does it behave like this, since I await the second setTimeout after displaying the first one?