最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

2个ForEach循环后的回调

IT培训 admin 3浏览 0评论

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
  }
);

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论