0
我有一個全局變量「結果」,它是一個字符串,它將被運行進程的兩個函數使用,並將其輸出附加到「結果」變量。 但是,這些函數需要時間來處理並返回結果。以下是代碼的當前格式。調用函數按順序存儲結果
var results = 'Results: ';
router.get('/',function(req,res){
app1.processImage(path, function(err, output){
console.log('app1 processing completed');
results = results+'app1 result:' + output;
});
app2.processImage(path, function(err, output){
console.log('app2 processing completed');
results = results+'app2 result:' + output;
});
console.log('Both processes are completed');
console.log(results);// none of the results printed.
});
當我嘗試打印結果時,它還沒有輸出。 程序產生這樣
電流輸出輸出:
Both the processes are completed
Results:
app2 processing completed
app1 processing completed
預期輸出:
app1 process completed
app2 process completed
Both the processes are completed
Results: app1 result : true , app2 result:true
注意,電流輸出結果是空的,因爲功能被尚未處理。我怎樣才能得到正確存儲在變量中的結果?
可能重複[如何從異步調用返回響應?](http://stackoverflow.com/questions/ 14220321 /如何-DO-I-返回的響應,從-的異步調用) –