gpt4 book ai didi

javascript - 具有复杂计算的异步回调Javascript

转载 作者:太空宇宙 更新时间:2023-11-04 15:32:40 25 4
gpt4 key购买 nike

我正在尝试异步进行一些计算,并在回调中获取计算结果。

function customWithCallBack(callback) {
//Complex logic calculation that takes time.
callback();
}

function actualCallBack() {
console.log("This is callback called after certain event");
}


console.log("First");
customWithCallBack(actualCallBack);
console.log("Second");

//Required Output
First
Second
This is callback called after certain event.


//Actual Output
First
This is callback called after certain event
Second

如何才能达到所需的输出?那是第一个“First is print”。然后,“第二”。最后在复杂逻辑完成后调用回调进行打印“这是在特定事件之后调用的回调”。除了网络请求和I/O操作之外,是否可以进行复杂的计算?

最佳答案

function customWithCallBack(callback) {
//Complex logic calculation that takes time.
setTimeout(function(){ callback(); }, 100);

}

function actualCallBack() {
console.log("This is callback called after certain event");
}


console.log("First");
customWithCallBack(actualCallBack);
console.log("Second");

关于javascript - 具有复杂计算的异步回调Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44669648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com