【发布时间】:2021-07-17 21:47:11
【问题描述】:
这些函数的输出以正确的顺序调用为 2、3、1,但我喜欢它先输出为 1,然后是 2、3。
const firstFunction = () => {
setTimeout(function() {
console.log(1);
}, 3000);
}
const secondFunction = () => {
console.log(2);
}
const thirdFunction = () => {
console.log(3);
}
firstFunction();
secondFunction();
thirdFunction();
我尝试使用回调,执行 1 后没有输出 2、3。
const firstFunction = () => {
setTimeout(function() {
console.log(1);
}, 3000);
}
const secondFunction = (callback) => {
console.log(2);
callback();
}
const thirdFunction = (callback) => {
console.log(3);
callback();
}
firstFunction(function() {
secondFunction(function() {
thirdFunction();
});
});
如何使用回调实现这一点?
【问题讨论】:
标签: javascript asynchronous callback