2个ForEach循环后的回调
我正在尝试使一个函数在2个foreach循环完成之后运行。
我尝试过函数回调并且没有运气
//Do the for each loops
array1.forEach(item1 => {
array3.push(item1)
})
array2.forEach(item2 => {
array3.push(item2)
})
// Then I want it to do a function when completed, at the moment it is doing the function before the foreach loops.
function();
我希望它在两个foreach循环都完成后执行一个函数!谢谢。
回答如下:您可以使用promises:
创建承诺:
var promise1 = new Promise(
function(resolve, reject) {
array1.forEach(item1 => {
array3.push(item1)
})
array2.forEach(item2 => {
array3.push(item2)
})
resolve({array1, array2});
}
);
处理退货:
promise1.then(
function(res){
//do anything with return
}
);
2个ForEach循环后的回调
我正在尝试使一个函数在2个foreach循环完成之后运行。
我尝试过函数回调并且没有运气
//Do the for each loops
array1.forEach(item1 => {
array3.push(item1)
})
array2.forEach(item2 => {
array3.push(item2)
})
// Then I want it to do a function when completed, at the moment it is doing the function before the foreach loops.
function();
我希望它在两个foreach循环都完成后执行一个函数!谢谢。
回答如下:您可以使用promises:
创建承诺:
var promise1 = new Promise(
function(resolve, reject) {
array1.forEach(item1 => {
array3.push(item1)
})
array2.forEach(item2 => {
array3.push(item2)
})
resolve({array1, array2});
}
);
处理退货:
promise1.then(
function(res){
//do anything with return
}
);