Quantcast
Viewing latest article 2
Browse Latest Browse All 2

nodejs request, loop & promise

I'm just starting with NodeJS. I try to do with NodeJS a loop and only then : send my result to an express template.

I tried many lib and promises but none of them worked. Node do "then" before ending the loop...

Here's my last try, can you help me with? Thanks a lot.

[...]//pveIds contains list of dailies id (object)var pveIds = body.pve;//init tab, will contain dailies titlevar pveNames = [];Promise.map(pveIds, function(pveId) {    // Promise.map awaits for returned promises as well.    request.get({        url: 'https://api.guildwars2.com/v2/achievements?id='+ pveId.id,        json: true      },      function(error, response, body) {        console.log('log 1: '+ body.name);        if (response.statusCode == 200) {          return body.name;        }      }).on('data', function(v) {      console.log('log 2: '+ v);      return v;    });  }).then(function(results) {    console.log("done");    console.log(results);    console.log("names tab:"+ pveNames);    res.render('pve.ejs', {      names: pveNames    });  });

Viewing latest article 2
Browse Latest Browse All 2

Trending Articles