练习题
求输出结果
console.log(1);
new Promise((resolve, reject) => {
console.log(2);
resolve();
}).then((res) => {
console.log(3);
});
console.log(4);
Details
查看答案
1,2,4,3setTimeout 和 Promise 的执行顺序
setTimeout(function () {
console.log(1);
}, 0);
new Promise(function (resolve, reject) {
console.log(2);
resolve();
})
.then(function () {
console.log(3);
})
.then(function () {
console.log(4);
});
console.log(6);
Details
查看答案
2,6,3,4,1setTimeout 和 Promise 的执行顺序
setTimeout(function () {
console.log(1);
}, 0);
new Promise(function (resolve, reject) {
console.log(2);
for (var i = 0; i < 100; i++) {
if (i === 10) {
console.log(10);
}
i == 99 && resolve();
}
console.log(3);
}).then(function () {
console.log(4);
});
console.log(5);
Details
查看答案
2,10,3,5,4,1求输出结果
console.log("start");
setTimeout(() => {
console.log("children2");
Promise.resolve().then(() => {
console.log("children3");
});
}, 0);
new Promise(function (resolve, reject) {
console.log("children4");
setTimeout(function () {
console.log("children5");
resolve("children6");
}, 0);
}).then((res) => {
// flag
console.log("children7");
setTimeout(() => {
console.log(res);
}, 0);
});
Details
查看答案
start,children4,children2,children3,children5,children7,children6请写出下面代码的运行结果
async function async1() {
console.log("async1 start");
await async2();
console.log("async1 end");
}
async function async2() {
console.log("async2");
}
console.log("script start");
setTimeout(function () {
console.log("setTimeout");
}, 0);
async1();
new Promise((resolve) => {
console.log("promise1");
resolve();
}).then(function () {
console.log("promise2");
});
console.log("script end");
查看答案
script start,async1 start,async2,promise1,script end
async1 end, promise2,setTimeout
将上一道题目变换一下,求输出
async function async1() {
console.log("async1 start");
await async2();
console.log("async1 end");
}
async function async2() {
new Promise(function (resolve) {
console.log("promise1");
resolve();
}).then(function () {
console.log("promise2");
});
}
console.log("script start");
setTimeout(function () {
console.log("setTimeout");
}, 0);
async1();
new Promise(function (resolve) {
console.log("promise3");
resolve();
}).then(function () {
console.log("promise4");
});
console.log("script end");
查看答案
script start,async1 start,promise1,promise3,script end
promise2,async1 end,promise4,setTimeout
将上面的题目变换一下,求输出
async function async1() {
console.log("async1 start");
await async2();
setTimeout(function () {
console.log("setTimeout1");
}, 0);
}
async function async2() {
setTimeout(function () {
console.log("setTimeout2");
}, 0);
}
console.log("script start");
setTimeout(function () {
console.log("setTimeout3");
}, 0);
async1();
new Promise(function (resolve) {
console.log("promise1");
resolve();
}).then(function () {
console.log("promise2");
});
console.log("script end");
查看答案
script start,async1 start,promise1,script end
promise2,setTimeout3,setTimeout2,setTimeout1
求输出结果
async function async1() {
console.log("a");
await async2();
console.log("b");
}
async function async2() {
console.log("c");
}
console.log("d");
async1();
setTimeout(() => {
console.log("e");
}, 0);
new Promise((resolve, reject) => {
console.log("f");
resolve();
}).then(() => {
console.log("g");
});
查看答案
d,a,c,f,b,g,e
求输出结果
console.log("1");
async function async1() {
console.log("2");
await async2();
console.log("3");
}
async function async2() {
console.log("4");
}
Promise.resolve().then(function () {
console.log("5");
});
setTimeout(function () {
console.log("6");
Promise.resolve().then(function () {
console.log("7");
});
new Promise(function (resolve) {
console.log("8");
resolve();
}).then(function () {
console.log("9");
});
});
async1();
new Promise(function (resolve) {
console.log("10");
resolve();
}).then(function () {
console.log("11");
});
console.log("12");
查看答案
1,2,4,10,12,5,3,11
6,8,7,9