以下是代码:
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行的callback()未定义。 TypeError:undefined不是函数。它很奇怪,因为它似乎并不关心adjust_geo中的那个?
答案 0 :(得分:1)
您的最后一行存在问题(您必须传递回调函数而不进行函数调用)并且您已将回调函数传递给所有函数,或者在传递回调时检入代码:
adjust_geo(
function(){
remove_stickies(
deposit_fluoros
)
});
这将通过回调调用 adjust_geo ,回调将使用将调用 deposit_fluoros 的回调调用 remove_stickies 。
如果Plasma.Hose('...').Deposit
看起来像Plasma.Hose('...').Deposit(data, doneCallback)
,您可以通过将下一个函数调用移动到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
)
});
答案 1 :(得分:0)
将您的最后一行更改为
adjust_geo(remove_stickies(deposit_fluoros));
当前正在进行此操作的方式,您传递的是deposit_fluoros的结果,而不是传递对该函数的引用。
答案 2 :(得分:0)
删除所有其他对象,只是简单的回调。放回你的物品,它会起作用。无论何时返回或调用回调,都要确保将其作为函数返回。
var adjust_geo = function (callback) {
callback();
console.log('adjust_geo');
}
// wrap the result of first call into a function and return it
var remove_stickies = function (callback) {
return function () {
callback()
console.log('remove_stickies');
};
}
var deposit_fluoros = function () {
console.log('deposit_fluoros')
};
adjust_geo(remove_stickies(deposit_fluoros));
输出:
"deposit_fluoros"
"remove_stickies"
"adjust_geo"