简体   繁体   English

Promise.promise的所有数组

[英]Promise.all array of promises

I have this code: 我有以下代码:

 db.group.findOne({_id: id}).then((groupFound) => {
     var membersArray = groupFound.members;

     Promise.all(membersArray.map((member) => {
         return db 
            .doneTodo
            .find({'victor._id': member._id})
            .then((userVictories) => {
                return {
                    email: member.email,
                    victories: userVictories.length
                }
            });
    })).then(function (result) {
        console.log(result);
    });

The db calls are all MongoDB/Mongoose. 数据库调用都是MongoDB / Mongoose。

The inside of the array returned from the map function is all promises. map函数返回的数组内部是所有promise。

The last call: 最后一个电话:

.then((userVictories) => {
    return {
        email: member.email,
        victories: userVictories.length
    }
})

returns a promise. 返回承诺。

So essentially, after this call is made, what I have is: 因此,从本质上讲,在进行此调用后,我得到的是:

[promise1, promise2, promise3, ... , promise x]

How are these promises concluded to change the array value to the value of promise1.then( (result)) ? 这些诺言如何得出结论以将数组值更改为promise1.then( (result))

How are these promises concluded to change the array value to the value of promise1.then( (result))? 如何完成这些承诺以将数组值更改为promise1.then((result))的值?

There's nothing special here. 这里没什么特别的。 The promises within the array will act like any promise does - they will resolve to some value after a period of time, result in an error after some period of time, or if they were set up poorly, never resolve or produce an error. 数组中的promise会像任何promise一样起作用-它们将在一段时间后解析为某个值,在一段时间后导致错误,或者如果它们的设置不正确,则永远不要解析或产生错误。

The purpose of Promise.all is simply to wait for all of the values in the array to resolve or for one to throw an error. Promise.all的目的仅仅是等待数组中的所有值解析或抛出一个错误。 If they all resolve, the promise returned from Promise.all will resolve to an array of all the results. 如果它们全部解决,则Promise.all返回的Promise.all将解析为所有结果的数组。 If one throws an error, it will produce that error. 如果有人抛出错误,它将产生该错误。

Promise.all isn't doing anything to the promises, just watching them. Promise.all对承诺没有做任何事情 ,只是在遵守它们。 We could create our own simple version of Promise.all like this: 我们可以创建自己的Promise.all版本,就像这样:

 function myAll(arr) { var resolvedCount = 0; var resultArray = []; return new Promise(function (resolve, reject) { if (arr.length === 0) { resolve([]); } arr.forEach(function (el, i) { // wrap el in a promise in case it's not already one Promise.resolve(el) .then( function (result) { // add result to the result array and increment the count resultArray[i] = result; resolvedCount += 1; if (resolvedCount === arr.length) { // all promises have resolved, // so this promise can resolve itself resolve(resultArray); } }, function (err) { // a promise threw an error. reject reject(err); } ); // end of .then() }); // end of .forEach() }); // end of new Promise() } // test out myAll() function log(value) { console.log(value); } function logError(value) { log('Error caught:' + value); } myAll([]) .then(log, logError); myAll([1, Promise.resolve('apple'), 5, Promise.resolve(7)]) .then(log, logError); myAll([2, 3, Promise.reject('no!')]) .then(log, logError); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM