我試着寫一個返回的異步函數的所有結果的功能,並執行推到一個數組的回調和記錄每一個異步函數的結果。JS:獲取異步函數內部函數參數和執行回調
由於把所有的菜時,他們都做了服務員。 我不明白如何獲得應該作爲結果返回的子參數。任務,我不工作的解決方案的代碼如下:
任務:
var dishOne = function(child) {
setTimeout(function() {
child('soup');
}, 1000);
};
var dishTwo = function(child) {
setTimeout(function() {
child('dessert');
}, 1500);
};
waiter([dishOne, dishTwo], function(results) {
console.log(results); // console output = ['soup', 'dessert']
});
我不工作的解決方案:
function child(arg) {
this.arr.push(arg)
}
function waiter(funcArray, doneAll) {
var result = {
arr: []
};
let i = 0;
const x = child.bind(result)
funcArray.forEach(function(f) {
f(x)
i++;
if(i == 2) {
doneAll(result.arr)
}
});
}
哎呀,對不起,我只是錯誤地理解了這個問題 – Octopitus