q promise and map doesn't change after iteration

23 views
Skip to first unread message

Erick Neverson

unread,
Jul 17, 2015, 5:02:00 PM7/17/15
to nod...@googlegroups.com

I'm using Q Promises to retrieve data from my redis repository. The problem I'm having, is that through each iteration, the array object (localEncounter) I'm using to store data returned from the chained functions is never updated at each iteration. Previously, I tried to solve this with a foreach loop and spread but the results were the same.

How should I correct this so that localEncounter is updated at each iteration, and ultimately localEncounters contains correct data when returned? Thank you.


var localEncounters = [];
            var localEncounter = {};

            Promise.all(ids.map(function(id) {
                return localEncounter, getEncounter(id, client)
                    .then(function (encounter) {
                        encounterObject = encounter;

                        //set the fields for the return object
                        localEncounter['encounterid'] = encounterObject[f_id];
                        localEncounter['screeningid'] = encounterObject[f_screening_id];
                        localEncounter['assessmentid'] = encounterObject[f_clinical_assessment_id];
                        localEncounter['psychevalid'] = encounterObject[f_psych_eval_id];

                        //get screening
                        return getScreening(encounterObject[f_screening_id], client);
                    })
                    .then(function (screening) {
                        //set the fields for the return object
                        localEncounter['screeningbegintime'] = screening[f_begin_time];
                        //get assessment                        
                        return getAssessment(localEncounter['assessmentid'], client);
                    })
                    .then(function (assessment) {
                        //set the fields for the return object
                        localEncounter['assessmentbegintime'] = assessment[f_begin_time];
                        //get psycheval
                        //localEncounters.push(assessment);
                        return getPsychEval(localEncounter['psychevalid'], client);
                    })
                    .then(function (psychEval) {
                        //set the fields for the return object
                        localEncounter['psychevalbegintime'] = psychEval[f_begin_time];

                        localEncounters.push(localEncounter);

                    }
                    , function (reason) {
                        console.log(reason); // display reason why the call failed;
                        reject(reason, 'Something went wrong creating the encounter!');
                    })

            })).then(function(results) {
                // results is an array of names
                console.log('done ');

                resolve(localEncounters);
            })

Ryan Graham

unread,
Jul 17, 2015, 5:33:43 PM7/17/15
to nod...@googlegroups.com
Is it your intention that each id iteration gets its own localEncounter object? If so, you'll want to move its declaration inside your map iterator. As it is, each id's chain is sharing the same object, and they are likely going through their phases at different times/speeds, so it's impossible to know the actual state of localEncounter at any given time.

Additionally, since the end of each chain pushes that object on to the localEncounters Array, that Array is filled entirely with references to a single object.

~Ryan


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/aeabd7f6-1200-47d9-8b10-469dd4fe4bc6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Erick Neverson

unread,
Jul 17, 2015, 10:42:19 PM7/17/15
to nod...@googlegroups.com
Ryan,

I was sooooo close man :)  Thanks, that actually worked for me.
Reply all
Reply to author
Forward
0 new messages