节点诺言循环返回诺言不起作用

时间:2019-05-03 09:35:58

标签: node.js

我是Node的新手,正在玩promise循环

我的循环完成(10次迭代)后,我想知道函数已经完成,该怎么办?

这是我的代码

var promise1 = new Promise((resolve, reject) => {
    for (let i = 0, p = Promise.resolve() ; i < 10; i++) {
        p = p.then(_ => 
            new Promise(resolve => {
                setTimeout(function () {
                console.log(i);
                resolve();
                }, 50);
            }
        ));
    }
});

//start
promise1.then(function (result) {
    // Resolve
    console.log("done");

}, function (result) {
    // Reject
    console.error(result);
});

3 个答案:

答案 0 :(得分:1)

promise1从未完成,因为您忘记为此呼叫resolve()reject()

请检查下面的代码段。我在添加或更改的行之前添加了评论。

// Use different names for the `resolve` callbacks to be clear which is which
var promise1 = new Promise((res) => {
    // Move the declaration of `p` outside the loop to let it be visible after the loop
    let p = Promise.resolve();

    for (let i = 0; i < 10; i++) {
        p = p.then(_ => 
            new Promise(resolve => {
                setTimeout(function () {
                console.log(i);
                resolve();
                }, 50);
            }
        ));
    }
    // Chain the completion of the outer promise when `p` resolves
    p.then(() => res());
});

//start
promise1.then(function (result) {
    // Resolve
    console.log("done");

}, function (result) {
    // Reject
    console.error(result);
});

答案 1 :(得分:0)

如果您想一次解决多个承诺,可以使用PrefabUtility.CreatePrefab()。 您可以做的是将每个承诺放入数组中,并使用如下所示的Promise.all函数来解决。

Promise.all

答案 2 :(得分:0)

function abc() {

  return new Promise(function (resolve, reject) {
        var events = [];
        for(var i=0;i<5;i++) {
            var event_arr = {};
            var event_city = {};
            event_arr.event_city = {"city":"m-"+i};
            
           events.push(event_arr);
        }
       if(events) {
         resolve(events)
       }else {
         reject("NOp");
       }
        
    }).then((events)=>{
          
            var p = Promise.resolve()
             var events2 = [];
              var i=0;
              events.forEach(function(element) {
                p = p.then(() => {
                  return new Promise(function (resolve, reject) {
                     var address_info = {};
                    var event_arr = {};
                    var event_city = element.event_city;
                    event_arr.event_cityy = event_city;
                    setTimeout(function () {
                      var address_info = {"address":"B-"+i};
                      console.log(i);
                      i++;
                      event_arr.address_info = address_info;
                      events2.push(event_arr);
                     // console.log(events2)
                      //resolve(events2);
                      if(events2) {
                        resolve(events2);
                      }else {
                        reject("NOp2");
                      }
                    }, 5000)
                  }).then((events2)=>{
                    return events2;
                  });
                })
                
            });
           return p.then((events2)=>{
              return events2;
            });
    });
}

var v = abc();
v.then(function(events2) {
   console.log(events2) 
   console.log("-------X------------")
})