不使用异步/等待顺序执行承诺数组

Sal*_*lar 6 javascript node.js promise

可以说我有很多承诺。我数组的每个元素都是一个knex.js查询生成器,可以执行并返回promise。

如何依次运行此数组的每个元素。该数组是动态构建的。

let promisesArray = [q1,q2,q3] ;
Run Code Online (Sandbox Code Playgroud)

每个q本身都不是一个承诺,但执行后将返回一个承诺。

Ami*_*ali 5

这可能是一个可能的选择:

let p = Promise.resolve([]);
promisesArray.forEach(q => {
  p = p.then(responses => {
    //based on the nature of each q, to start execution
    //use either q().then() or q.then()
    return q().then(response => {
      //Any further logic can be here.
      console.log(response);
      return responses.concat([response]);
    })
  })
})

p.then(responses => {
  // here you have all of the responses.
})
Run Code Online (Sandbox Code Playgroud)