下面是代碼:如何執行3個異步函數?
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()));
,你可以在最後一行看到,我試圖以執行三個asnychronous功能。然而,現在它抱怨第34行的callback()是未定義的。 TypeError:undefined不是一個函數。它很奇怪,因爲它似乎不關心adjust_geo中的那個?
承諾是要走的路。避免異步函數的回調,它會很快讓你感到困惑。 – simonzack 2014-08-28 18:41:48
你在34上得到錯誤的原因是因爲callback()在remove_stickies函數外被調用。 – 2014-08-28 18:46:31
'adjust_geo','remove_stickies'和'deposit_fluoros'的執行順序是什麼? – Igor 2014-08-28 19:52:44