it is expected that the following code should be printed in the order of 1 2 3 4 5, but when it is actually executed, it is 1 2 5 3 4 4
(async () => {
console.log("1");
let n = await new Promise((reslove, reject) => {
console.log("2");
setTimeout(() => {
reslove("3");
}, 2000);
});
console.log(n);
console.log("4");
})();
console.log("5");
it is expected that await new Promise will wait until reslove ("3"). If it is executed sequentially, how can it be skipped directly? How to adjust if you want to implement 1 2 3 4 5?
Note
.babelrc
{
"presets": [
[
"env",
{
"targets": {
"node": "current"
}
}
]
],
"plugins": ["transform-object-rest-spread"],
"ignore": ["node_modules"]
}