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


On Fri, 17 Jul 2015 at 14:01 Erick Neverson <[email protected]>
wrote:

>
>
> <http://stackoverflow.com/questions/31481546/q-promise-and-map-doesnt-change-after-iteration#>
>
> 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);
>             })
>
>  --
> 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 [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/aeabd7f6-1200-47d9-8b10-469dd4fe4bc6%40googlegroups.com
> <https://groups.google.com/d/msgid/nodejs/aeabd7f6-1200-47d9-8b10-469dd4fe4bc6%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CAGjmZGzPd%2B-jQ4rKTa2s0KZfXH_ZVY-G66BDnYpQTtQnOPA2mw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to