Jim from Princeton
Jim from Princeton

Reputation: 741

JavaScript Promises - Creating an array of promises to be executed together

I'm working with Node.js on async calls to noSQL DynamoDB. I first query to see which 'buddy list(s)' the account belongs. That may return from zero to 'n' new Primary Keys that will contain lists of all of the members of each of those buddy lists. Think of them as clubs to which a person belongs... you may have none or many; each club has several members or even one.

So far (and I am working with Promises for the first time here... though I have used callbacks on prior JA projects) I'm OK with where I am, but I know that I am assembling the array of promises incorrectly. That is, I can see in the console that the .then function executes before both promises resolve. For the record, I do expect that... it seems reasonable that any .then may be satisfied with a single promise resolving.

Anyways, Can someone offer up some tips as to how to structure? I'd like to end up with pseudo:

getBuddyLists .then(getAllTheBuddiesInTheLists) .then(function(){//doSomething});

my Node.js code:

const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region:'us-east-1'});
var buddyGroups = [];

exports.handler = function(event, context, callback) {
    var params = {
        TableName: 'USER_INFORMATION',
        Key: {
            "DEVICE_ID": event.DEVICE_ID
        }
    };

    var getBuddyList = new Promise(function(resolve, reject){
        docClient.get(params, function(err, data){
            if(err){
                reject(err);
            }
            else{
                var dataJSON = JSON.parse(JSON.stringify(data));
                dataJSON.Item.my_buddies.values.forEach(function(value){
                    buddyGroups.push(value);
                });
                console.log('Got buddy groups: ' + buddyGroups);
                resolve(buddyGroups);
            }
        });
    });

    getBuddyList.then(function(capturedData){  // capturedData => an array of primary keys in another noSQL document
        console.log('groups: ' + capturedData);
        var myPromises = [];
        capturedData.forEach(function(value){
            var reqParams = {
                        TableName: "buddy_list",
                        Key: {"BUDDY_ID": value}
                    };
            myPromises.push(new Promise (function(resolve, reject){
                docClient.get(reqParams, function(err, data){
                    if(err){
                        //console.log(err, data);
                    reject(err);
                    }else{
                        var returnedJSON = JSON.parse(JSON.stringify(data));
                        console.log(returnedJSON);
                        resolve(returnedJSON);
                    }
                });
            }));
        });
        //
        Promise.all(myPromises); // ADDED IN EDIT <<<<<<<<<<<<<<<<<<<<<<
        // how to make sure all of myPromises are resolved?
        //
    }).then(function(){
            console.log("done");
        })
    .catch(function(err){
        console.log("error message:" + error);
    });
};

EDIT: Added location of Promise.all(myPromises);

Upvotes: 9

Views: 23957

Answers (2)

Jaromanda X
Jaromanda X

Reputation: 1

Just to clarify how you would use Promise.all, even though you accepted an answer, you edited the question, but still are not using it correctly

Also, the discussion of .map vs .forEach - this is how you would use .map

    getBuddyList.then(function(capturedData){  // capturedData => an array of primary keys in another noSQL document
        console.log('groups: ' + capturedData);
        // changes to use .map rather than .forEach
        // also, you need to RETURN a promise - your edited code did not
        return Promise.all(capturedData.map(function(value){
            return new Promise (function(resolve, reject){
                var reqParams = {
                    TableName: "buddy_list",
                    Key: {"BUDDY_ID": value}
                };
                docClient.get(reqParams, function(err, data){
                    if(err){
                        //console.log(err, data);
                    reject(err);
                    }else{
                        var returnedJSON = JSON.parse(JSON.stringify(data));
                        console.log(returnedJSON);
                        resolve(returnedJSON);
                    }
                });
            });
        }));
    }).then(function(){
            console.log("done");
        })
    .catch(function(err){
        console.log("error message:" + error);
    });

Upvotes: 12

SLaks
SLaks

Reputation: 887195

You're looking for Promise.all(myPromises), which returns a single promise of an array of the results of the promises.

Upvotes: 2

Related Questions