【问题标题】:Bluebird Promisfy.each, with for-loops and if-statements?Bluebird Promisfy.each,带有 for 循环和 if 语句?
【发布时间】:2014-09-26 10:05:13
【问题描述】:

现在,父 for 循环 (m < repliesIDsArray.length) 在第一个 findOne 触发之前完成,所以这一切只循环通过 repliesIDsArray..asynchronous..的最后一个元素..

此代码集的承诺版本的正确语法是什么?我是 Promisification 的新手,想知道如何开始这个 Promisify + 遍历数组 + 考虑 if 语句..

需要Bluebird,并调用Promise.promisifyAll(require("mongoose"));

for(var m=0; m<repliesIDsArray.length; m++){

objectID = repliesIDsArray[m];

Models.Message.findOne({ "_id": req.params.message_id},
    function (err, doc) {
        if (doc) {
         // loop over doc.replies to find the index(index1) of objectID at replies[index]._id
         var index1;
         for(var i=0; i<doc.replies.length; i++){
            if (doc.replies[i]._id == objectID) {
                index1 = i;
                break;
            }
         }
         // loop over doc.replies[index1].to and find the index(index2) of res.locals.username at replies[index1].to[index2]
         var index2;
         for(var j=0; j<doc.replies[index1].to.length; j++){
            if (doc.replies[index1].to[j].username === res.locals.username) {
                index2 = j;
                break;
            }
         }

         doc.replies[index1].to[index2].read.marked = true;
         doc.replies[index1].to[index2].read.datetime = req.body.datetimeRead;
         doc.replies[index1].to[index2].updated= req.body.datetimeRead;
         doc.markModified('replies');
         doc.save();
    }
}); // .save() read.marked:true for each replyID of this Message for res.locals.username

} // for loop of repliesIDsArray

【问题讨论】:

  • 您可以使用Promise.each.fineOneAsync
  • @BenjaminGruenbaum 感谢您的提醒.. 测试这个:Promise.each(function(repliesIDsArray) { console.log('is repliesIDsArray here now equivalent to repliesIDsArray[i] ? ' + repliesIDsArray ); }); 虽然记录了这个:Possibly unhandled TypeError: fn must be a function .. 肯定会很感激一个如何开始这个的例子

标签: node.js mongoose promise node-mongodb-native bluebird


【解决方案1】:

正如本杰明所说,不要使用for循环,而是使用Promise.each(或.map

查看 Bluebird API 文档 here 并搜索“静态地图示例:”。 mapeach 的文档更容易理解

var Promise = require('bluebird')
// promisify the entire mongoose Model
var Message = Promise.promisifyAll(Models.Message)

Promise.each(repliesIDsArray, function(replyID){
    return Message.findOneAsync({'_id': req.params.message_id})
        .then(function(doc){
            // do stuff with 'doc' here.  
        })
})

从文档中,.each(或.map)采用“an array, or a promise of an array, which contains promises (or a mix of promises and values)”,这意味着您可以将它与 100% 纯值数组一起使用来启动承诺链

希望对你有帮助!

【讨论】:

  • 这样不行,必须在.each的函数中返回promise,否则无法知道promise已经完成。如果您对文档有任何建议,我很乐意听到并进行相应的编辑。
  • 感谢我纠正的好问题。文档很棒,我唯一要添加的是 .each 的“静态”示例。我将他指向“静态地图”部分,因为它具有使用数组启动 promise 的示例
  • @BenjaminGruenbaum 谢谢你,现在完美运行
  • 很高兴我能帮上忙。享受 Bluebird,如果您有任何反馈、建议等,我们很乐意听到。
  • 感谢您的快速回复@aarosil。实际上它工作正常。我在代码中有其他问题。现在一切正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 2013-02-06
  • 1970-01-01
相关资源
最近更新 更多