我有下面的代码
function run(){
one();
two();
three();
}
function one(){
console.log(1);
}
function two(){
setTimeout(function(){
console.log(2);
});
}
function three(){
console.log(3);
}
run();
以下代码的输出将是1,3,2
我们必须在代码中做哪些更改才能使输出为 1,2,3
?
最佳答案
这应该有效:
async function run() {
await one();
await two()
await three()
}
function one() {
Promise.resolve(
console.log(1)
)
}
function two() {
return new Promise(resolve => {
setTimeout(() => {
console.log(2);
resolve();
});
});
}
function three() {
Promise.resolve(
console.log(3)
)
}
run();
关于javascript - 更改 JavaScript 中的执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62732788/