Re: [nodejs] Re: Synchronous for loop

2017-10-27 Thread Mark Volkmann
You are using the loop index i inside a function that is invoke asynchronously. 
Try changing this:

console.log('response2-->',response[i]);

to this:

console.log('response2-->',response[0]);

---
R. Mark Volkmann
Object Computing, Inc.

> On Oct 27, 2017, at 12:54 PM, gupta.rup...@gmail.com wrote:
> 
> Thank you for the response. Below is my code:
> for(var i=0;i var messageTimeStamp = response[i].messageTimeStamp;
> console.log('messageTimeStamp-->'+messageTimeStamp);
> var body = {deviceId:req.body.deviceId};
> console.log('response1-->',response[i]);
> Custody.find(body, function(error, custodyResponse) {
> if(custodyResponse.length == 1){
>   console.log('custodyResponse.length-->'+custodyResponse.length);
>   console.log('response2-->',response[i]);
> 
>   }
> 
> });
> 
> here I am getting value for response1 but not for response2
> 
> Regards
> 
>> On Tuesday, October 24, 2017 at 4:57:58 PM UTC-4, gupta@gmail.com wrote:
>> Hi 
>>  I am new to node js. I am facing an issue. 
>> I have 3 ids. I am iterating in for loop. For each value I am calling web 
>> service. The web service is find by id by passing each id. What I noticed is 
>> that it always takes the last id.
>> for eg: id are 10, 20, 30
>> When I call any service by passing in for loop it always takes 30.
>> Any help is appreciated.
>> 
>> Regards
> 
> -- 
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to nodejs@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/nodejs/572a0df8-4ecf-43e0-9ee4-fe3757bc8cd1%40googlegroups.com.
> 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 nodejs+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/D7E70427-F29B-42D7-AA04-E2731306B5D8%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


[nodejs] Re: Synchronous for loop

2017-10-27 Thread gupta . rupesh
Thank you for the response. Below is my code:
for(var i=0;i'+messageTimeStamp);
var body = {deviceId:req.body.deviceId};
console.log('response1-->',response[i]);
Custody.find(body, function(error, custodyResponse) {
if(custodyResponse.length == 1){
  console.log('custodyResponse.length-->'+custodyResponse.length);
  console.log('response2-->',response[i]);

  }

});

here I am getting value for response1 but not for response2

Regards

On Tuesday, October 24, 2017 at 4:57:58 PM UTC-4, gupta@gmail.com wrote:
>
> Hi 
>  I am new to node js. I am facing an issue. 
> I have 3 ids. I am iterating in for loop. For each value I am calling web 
> service. The web service is find by id by passing each id. What I noticed 
> is that it always takes the last id.
> for eg: id are 10, 20, 30
> When I call any service by passing in for loop it always takes 30.
> Any help is appreciated.
>
> Regards
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/572a0df8-4ecf-43e0-9ee4-fe3757bc8cd1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [nodejs] Re: Synchronous for loop

2017-10-25 Thread Arun Antony
Use each method in underscore library.

On 25 Oct 2017 7:04 pm, "Mark Volkmann"  wrote:

> There is nothing async in your loop, so it doesn’t make sense that you
> could get the same output from each iteration unless every object in
> dResponse is the same. Are you saying that you get the value of dResponse
> from an async call? You didn’t show that part.
>
> Also, you are making the code to iterate through dResponse too
> complicated. Try something like this:
>
> for (const res of dResponse) {
>   const {dId} = res;
>   console.log(‘did =‘, dId);
>   const dUrl = `${url}?id=${dId}`;
>   console.log(‘dUrl =‘, dUrl);
> }
>
> ---
> R. Mark Volkmann
> Object Computing, Inc.
>
> On Oct 24, 2017, at 8:53 PM, gupta.rup...@gmail.com wrote:
>
> Here is my code. Its a simple one
>  for(var a=0; a var dId = dResponse[a].dId;
> console.log('dId-->' +dResponse);
>   var dUrl = url?id=' + dId;
>   console.log('Url:'+dUrl);
> }
> Every time it returns the same url.
>
> On Tuesday, October 24, 2017 at 4:57:58 PM UTC-4, gupta@gmail.com
> wrote:
>>
>> Hi
>>  I am new to node js. I am facing an issue.
>> I have 3 ids. I am iterating in for loop. For each value I am calling web
>> service. The web service is find by id by passing each id. What I noticed
>> is that it always takes the last id.
>> for eg: id are 10, 20, 30
>> When I call any service by passing in for loop it always takes 30.
>> Any help is appreciated.
>>
>> Regards
>>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to nodejs@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/nodejs/0fab98fb-38e4-4aeb-bf65-d899fd08d0d9%40googlegroups.com
> 
> .
> 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 nodejs+unsubscr...@googlegroups.com.
> To post to this group, send email to nodejs@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/nodejs/07723C4D-E2BD-48D1-826C-7FFCA51C3692%40gmail.com
> 
> .
> 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 nodejs+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CAB3m6Y3KfWUnk2hyw0H8ZTQ3ihLYC11YPhXLkd-oeh8rRWnWiQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[nodejs] Re: Synchronous for loop

2017-10-25 Thread Zlatko


On Wednesday, October 25, 2017 at 7:38:37 AM UTC+2, gupta@gmail.com 
wrote:
>
> Here is my code. Its a simple one
>  for(var a=0; a var dId = dResponse[a].dId;
> console.log('dId-->' +dResponse);
>   var dUrl = url?id=' + dId;
>   console.log('Url:'+dUrl);
> }
> Every time it returns the same url.
>
>>
>>

You didn't show the whole code - what's in dResponse array? But, your title 
says "Synchronous for loop", depending on your node version, here's a 
simple example:

const request = require('request');

function callRemoteApi(url) {
  return new Promise((resolve, reject) => {
console.log('Fetching', url);
request(url, (err, res) => {
  if (err) {
return reject(err);
  }
  console.log('Got', url);
  const result = JSON.parse(res.body).url;
  resolve(result);
});
  });
}
async function runTest() {
  const urls = [
'https://httpbin.org/delay/5',
'https://httpbin.org/delay/1',
'https://httpbin.org/delay/3',
  ];
  console.log('Urls ready.');
  let promises = [];
  for (let url of urls) {
promises.push(callRemoteApi(url));
  };
  console.log('Promises ready.');
  await Promise.all(promises.map(async promise => console.log('Result of 
promise:' + await promise)))
.then(() => console.log('All done.'));
}

runTest();

Now, if you run this code, it'll show you how things are working.

So the for loop example is indicative of potential issues with sync for 
loops. It takes an url, *waits* for it to return, and then goes on to the 
next.
Now, the other example works much better: we execute all promises 
immediately, in parallel. And as they come back, we get the results logged 
immediately.

Not sure how applicable this is to your situation, but if you post more 
code, we might help out.



 

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/5f91c74d-12e6-499e-b7ad-6918048604dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [nodejs] Re: Synchronous for loop

2017-10-25 Thread Mark Volkmann
There is nothing async in your loop, so it doesn’t make sense that you could 
get the same output from each iteration unless every object in dResponse is the 
same. Are you saying that you get the value of dResponse from an async call? 
You didn’t show that part.

Also, you are making the code to iterate through dResponse too complicated. Try 
something like this:

for (const res of dResponse) {
  const {dId} = res;
  console.log(‘did =‘, dId);
  const dUrl = `${url}?id=${dId}`;
  console.log(‘dUrl =‘, dUrl);
}

---
R. Mark Volkmann
Object Computing, Inc.

> On Oct 24, 2017, at 8:53 PM, gupta.rup...@gmail.com wrote:
> 
> Here is my code. Its a simple one
>  for(var a=0; a var dId = dResponse[a].dId;
> console.log('dId-->' +dResponse);
>   var dUrl = url?id=' + dId;
>   console.log('Url:'+dUrl);
> }
> Every time it returns the same url.
> 
>> On Tuesday, October 24, 2017 at 4:57:58 PM UTC-4, gupta@gmail.com wrote:
>> Hi 
>>  I am new to node js. I am facing an issue. 
>> I have 3 ids. I am iterating in for loop. For each value I am calling web 
>> service. The web service is find by id by passing each id. What I noticed is 
>> that it always takes the last id.
>> for eg: id are 10, 20, 30
>> When I call any service by passing in for loop it always takes 30.
>> Any help is appreciated.
>> 
>> Regards
> 
> -- 
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to nodejs@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/nodejs/0fab98fb-38e4-4aeb-bf65-d899fd08d0d9%40googlegroups.com.
> 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 nodejs+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/07723C4D-E2BD-48D1-826C-7FFCA51C3692%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


[nodejs] Re: Synchronous for loop

2017-10-24 Thread gupta . rupesh
Here is my code. Its a simple one
 for(var a=0; a' +dResponse);
  var dUrl = url?id=' + dId;
  console.log('Url:'+dUrl);
}
Every time it returns the same url.

On Tuesday, October 24, 2017 at 4:57:58 PM UTC-4, gupta@gmail.com wrote:
>
> Hi 
>  I am new to node js. I am facing an issue. 
> I have 3 ids. I am iterating in for loop. For each value I am calling web 
> service. The web service is find by id by passing each id. What I noticed 
> is that it always takes the last id.
> for eg: id are 10, 20, 30
> When I call any service by passing in for loop it always takes 30.
> Any help is appreciated.
>
> Regards
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/0fab98fb-38e4-4aeb-bf65-d899fd08d0d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.