gpt4 book ai didi

javascript - 如何按顺序执行3个异步函数?

转载 作者:行者123 更新时间:2023-12-03 11:57:12 26 4
gpt4 key购买 nike

代码如下:

adjust_geo = function(callback){Plasma.Hose('edge-to-sluice').Deposit({
"descrips": [
"sluice",
"prot-spec v1.0",
"request",
"zoom"
],
"ingests": {
"lat": stickie.latitude,
"lon": stickie.longitude,
"level": stickie.zoom_level
}
});
Plasma.Hose('edge-to-sluice').Deposit({
"descrips": [
"sluice",
"prot-spec v1.0",
"request",
"set-time"
],
"ingests": {
"time": stickie.start,
"rate": 2.0,
"pause": false
}
});
callback();
}

var remove_stickies = function(callback){
Plasma.Hose('edge-to-sluice').Deposit({ descrips: ['sluice', 'prot-spec v1.0', 'request', 'remove-all-fluoro'], ingests : {}
});
callback();
}


deposit_fluoros = function(){
console.log(stickie.fluoroscopes);
L = JSON.parse(stickie.fluoroscopes);
console.log(L);
for (var i = 1; i<L.length;i++){
console.log(L[i]);
Plasma.Hose('tcp://localhost/edge-to-sluice')
.Deposit({
"descrips": [
"sluice",
"prot-spec v1.0",
"request",
"new-fluoro-instance"],
"ingests":L[i]
});
};
}
}
adjust_geo(remove_stickies(deposit_fluoros()));

正如您在最后一行所看到的,我正在尝试按顺序执行三个异步函数。然而现在它提示第 34 行的回调() 未定义。类型错误:未定义不是函数。这很奇怪,因为它似乎并不关心 adjustment_geo 中的那个?

最佳答案

您的最后一行有问题(您必须传递回调函数而不是进行函数调用),并且您已将回调传递到所有函数中或检查代码(如果传递了回调):

adjust_geo(
function(){
remove_stickies(
deposit_fluoros
)
});

这将调用 adjust_geo ,回调将调用 remove_stickies ,回调将调用 deposit_flus

如果 Plasma.Hose('...').Deposit 看起来像 Plasma.Hose('...').Deposit(data, didCallback) 你可以通过将下一个函数调用移至 doneCallback 来更新代码以按顺序运行函数:

adjust_geo = function(callback) {
Plasma.Hose('edge-to-sluice').Deposit({
"descrips": [
"sluice",
"prot-spec v1.0",
"request",
"zoom"
],
"ingests": {
"lat": stickie.latitude,
"lon": stickie.longitude,
"level": stickie.zoom_level
}
},
function() {
Plasma.Hose('edge-to-sluice').Deposit({
"descrips": [
"sluice",
"prot-spec v1.0",
"request",
"set-time"
],
"ingests": {
"time": stickie.start,
"rate": 2.0,
"pause": false
}
}, callback)
}
);
}

var remove_stickies = function(callback) {
Plasma.Hose('edge-to-sluice').Deposit({
descrips: ['sluice', 'prot-spec v1.0', 'request', 'remove-all-fluoro'],
ingests: {}
}, callback);
}


deposit_fluoros = function() {
console.log(stickie.fluoroscopes);
L = JSON.parse(stickie.fluoroscopes);
console.log(L);
for (var i = 1; i < L.length; i++) {
console.log(L[i]);
Plasma.Hose('tcp://localhost/edge-to-sluice')
.Deposit({
"descrips": [
"sluice",
"prot-spec v1.0",
"request",
"new-fluoro-instance"
],
"ingests": L[i]
});
};
}
}

adjust_geo(
function() {
remove_stickies(
deposit_fluoros
)
});

关于javascript - 如何按顺序执行3个异步函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25555770/

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