节点诺言链不能同步执行吗?

时间:2019-02-21 06:02:24

标签: node.js promise

如果我有一个promise / function序列,例如下面的示例,函数是否仍然有可能按顺序分解?我有一个函数是猫鼬查询并更新,然后另一个函数将数据发送到视图。我的视图正在加载,但不能反映猫鼬查询结果的正确结果。

let function_one(data) = function(){
  return new Promise( function(resolve, reject) {  
    {do stuff...    
      resolve('howdy');
    }
  });
};

let function_two(some_data) = function(){
  return new Promise( function(resolve, reject) {  
    {  do stuff...    
    resolve('howdy');
    }
  });
};  

let function_three(some_data) = function(){
  return new Promise( function(resolve, reject) {  
    {do stuff...    
      resolve('howdy');
    }
  });
};  

let function_four(some_data) = function(){
  return new Promise( function(resolve, reject) {  
    {do stuff...    
      resolve('howdy');
    }
  });
};  

function_one(data).then(function(result){
  return function_two(result);
}).then(function(result){
  return function_three(result);
}).then(function(result){
  return function_four(result);
}).then(function(result){
  console.log(result);
}).catch(err => console.log("Caught " + err));

这是我整理的代码:就像“ res.json({status:200,data:result});”在猫鼬查找完成之前被执行?

let gettheinformation =  function(invoicelist3){
    return new Promise(  function(resolve, reject) {
        // console.log(invoicelist3);
            Product.aggregate([
                {
                    $match: {
                        Priced: true
                    }
                }
                ,
                {
                    $group: {
                        _id: "$Invoice",
                        pricedcount: {$sum: 1}
                    }
                }
            ], function (err, result) {
                if (err) {
                    console.log(err);
                } else {
                    resolve(result);
                }
            });
    });
};


let getinvprodspriced =  function(invlist){
    return new Promise(  function(resolve, reject) {
           // console.log(invlist);
        for(var oo = 0; oo < invlist.length; oo++){
        Invoicestatus.update({Invoice:invlist[oo]._id}, {Noofitemspriced: invlist[oo].pricedcount}, {upsert: true},  function (err) {}); 
        }
        resolve(invlist);
        });
};

let getinvprodcount = function(invprodcount){
        return new Promise(  function(resolve, reject) {
    Product.aggregate([
                {
                    $group: {
                        _id: "$Invoice",
                        pradcount: {$sum: 1}
                    }
                }
            ], function (err, result) {
                if (err) {
                    console.log(err);
                } else {
                  // console.log(result);
                    resolve(result);
                }
            });

    });
}


let saveinvprodcount =  function(invprodcount){
    return new Promise(  function(resolve, reject) {
        for(var ok = 0; ok < invprodcount.length; ok++){
        Invoicestatus.update({Invoice:invprodcount[ok]._id}, {Noofitems: invprodcount[ok].pradcount}, {upsert: true},  function (err) {}); 
        }
        resolve(invprodcount);
        });
};

let getarrdocs =  function(result){
    return new Promise( function(resolve, reject) {
        Invoicestatus.find({}, function(err, docs){
          resolve(docs); 
        });       
    });
};

router.get('/fetcharrdata', function(req, res) {


    gettheinformation().then(function(result){
      return getinvprodspriced(result);
    }).then(function(result){
      return getinvprodcount(result);
    }).then(function(result){
      return saveinvprodcount(result);
    }).then( function(result){
      return getarrdocs(result);
    }).then(function(result){


        res.json({status: 200, data: result});


    }).catch(err => console.log("Caught " + err));

});

2 个答案:

答案 0 :(得分:0)

是的。你可以做。通过使用Promise.all([promise1, promise2, promise3])让它们异步运行并立即获得结果。但是,在这种情况下,第一个调用的结果是输入,您需要在运行第二个调用之前等待它。因此,您必须按顺序运行(因为业务逻辑,而不是语言的本质。

答案 1 :(得分:0)

问题出在用于循环的saveinvprodcount方法中,该函数将在执行Invoicestatus.update函数之前解决,

let saveinvprodcount =  function(invprodcount){
return new Promise(  function(resolve, reject) {
    for(var ok = 0; ok < invprodcount.length; ok++){
    Invoicestatus.update({Invoice:invprodcount[ok]._id}, {Noofitems: invprodcount[ok].pradcount}, {upsert: true},  function (err) {}); 
    }
 //this will resolve before completion of Invoicestatus.update
    resolve(invprodcount);
    });
};

首先使用promisify更新函数,然后使用数组保存诺言,然后可以使用promise.all一次执行所有诺言。见下文

 let saveinvprodcount = function (invprodcount) {
 let promises = [];
 for (var ok = 0; ok < invprodcount.length; ok++) {
     promises.push(updateData(invprodcount[ok]));
 }
 return Promise.all(promises);
 };

 let updateData = function (invProd) {
 return new Promise(function (resolve, reject) {
    Invoicestatus.update({ Invoice: invProd._id },
        { Noofitems: invProd.pradcount }, { upsert: true }, function (err) {
            if (err)
                reject(err);
            else
                resolve();
        });
     });
   };