Bluebird Promisfy.each,带有 for 循环和 if 语句? [英] Bluebird Promisfy.each, with for-loops and if-statements?

查看:21
本文介绍了Bluebird Promisfy.each,带有 for 循环和 if 语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Right now, the parent for-loop (m < repliesIDsArray.length) completes before the first findOne fires, so this all only loops through the last element of the repliesIDsArray..asynchronous..

此代码集的承诺版本的正确语法是什么?我是 promisification 的新手,想知道如何开始这个 promisify + 循环数组 + if 语句的帐户..

What's the proper syntax for a promisified version of this codeset? Am new to promisification, and wondering how to start this promisify + loop through arrays + account for if-statements..

Bluebird 是必需的,Promise.promisifyAll(require("mongoose")); 被调用.

Bluebird is required, and Promise.promisifyAll(require("mongoose")); is called.

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

推荐答案

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

As Benjamin said, instead of using for loop, use Promise.each (or .map)

查看 Bluebird API 文档此处并搜索示例静态地图:".mapeach

Look on the Bluebird API docs here and search "example of static map:". With map is clearer to understand than docs for each

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)采用一个数组,或一个数组的承诺,其中包含承诺(或混合承诺和价值)",这意味着你可以将它与 100% 纯值数组一起使用来启动承诺链

From the docs, .each (or .map) takes "an array, or a promise of an array, which contains promises (or a mix of promises and values)", so that means you can use it with array of 100% pure values to kickoff promise chain

希望能帮到你!

这篇关于Bluebird Promisfy.each,带有 for 循环和 if 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆