返回列表

Promise.all嵌套

默认分类 2017/12/28 23:33

示例如下

var list11 = [1, 5, 3, 4]
var que11 = []
for (var i = 0; i < list11.length; i++) {
  que11.push(new Promise(function(resolve) {
    window.setTimeout(function(i) {
      console.log(i)
      resolve("Hello" + i)
    }.bind({}, list11[i]), 1000 * list11[i])
  }))
}

var list22 = [1, 8, 3, 4]
var que22 = []
for (var i = 0; i < list22.length; i++) {
  que22.push(new Promise(function(resolve) {
    window.setTimeout(function(i) {
      console.log(i)
      resolve("Hello" + i)
    }.bind({}, list22[i]), 1000 * list22[i])
  }))
}

Promise.all([
  Promise.all(que11),
  Promise.all(que22)
]).then(function(result) {
  console.log(result) 
})