Can someone help explain to me why is it behave this way?
To the best of my knowledge, adding the await sleep(1)
line should not affect the code flow in this case. However, it does.
function sleep(time) {
return new Promise((r) => setTimeout(r, time));
}
async function test(target) {
const ids = { a: ['a1', 'a2'], b: ['b3', 'b4'] }[target];
for (id of ids) {
console.log('X.', target, id);
// await sleep(1);
console.log('Y.', target, id);
}
}
test('a');
test('b');
Why?
Thanks!