Current Output is:1 3 2,
Expected Output is: 1 2 3
Conditions are: Don't use setTimeout again and can not change the order of the console log. Pl help.
console.log(1);
setTimeout(() => {
console.log(2)
})
console.log(3);
Here are two ways - with and without await, but both involving a Promise.
awaitasync function foo() {
console.log(1);
await new Promise(resolve => {
setTimeout(() => {
console.log(2);
resolve();
}, 10)
});
console.log(3);
}
foo();
await (using .then)console.log(1);
new Promise(resolve => {
setTimeout(() => {
console.log(2);
resolve();
}, 10)
}).then(() => {
console.log(3);
});