我正在尝试运行此代码片段,但它似乎没有执行回调。我正在尝试了解发生了什么事。
function exampleSimpleTask(done) {
console.log("task", "Before "+new Date().getTime());
setTimeout(() => done, 2000);
}
function demo() {
alert("Demo Executed");
}
exampleSimpleTask(demo); // it doesn't do anything.
// exampleSimpleTask.call(null, demo);
// exampleSimpleTask.call(null, demo.bind(this));
我不确定为什么执行回调,我怀疑它与箭头函数有关。如果将 =>
替换为 function()
会发生什么?
最佳答案
您忘记在 lambda 表达式中调用 done()
函数。下面的代码应该可以工作:
function exampleSimpleTask(done) {
console.log("task", "Before "+new Date().getTime());
setTimeout(() => done(), 2000);
}
function demo() {
alert("Demo Executed");
}
exampleSimpleTask(demo); // it doesn't do anything.
// exampleSimpleTask.call(null, demo);
// exampleSimpleTask.call(null, demo.bind(this));
关于Javascript 回调未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52938045/