There is an object with data:

obj.user = { "10": { "500": { "rank": "LEADER", "nick": "test1" }, "501": { "rank": "MEMBER", "nick": "test2" } }, "20": { "500": { "rank": "LEADER", "nick": "test1" } } } 

Further function

 var userCache = {} function checkUser(chat, id, callback) { if(userCache[id] === undefined) { request(apiURL, function(err, res, body) { // возвращается json userCache[id] = body; console.log(id + " NEW"); callback(); }) } else { console.log(id + " OLD") } } 

How to make elements go with obj.user to a function in turn and userCache remembers everything correctly. I wrote this code here:

 Object.keys(obj.user).reduce((promiseChain, chat) => { return promiseChain.then(() => new Promise((resolve) => { for(var id in obj.user[chat]) { checkUser(chat, id, resolve); } })); }, Promise.resolve()); 

but for some reason it processes about 40 users and stops.

  • where in the checkUser function the third parameter is used, to which you pass resolve ? - Grundy
  • I forgot to add, I apologize. corrected. - teamspam

1 answer 1

In this case, the use of the Promise constructor is not entirely correct. Since the promise can go into the state of resolve , only once, calling the resolve method in the loop will not do anything.

instead, you need to create a Promise at each iteration of the loop, and then just wait for them all, using Promise.all

 Object.keys(obj.user).reduce((promiseChain, chat) => { return promiseChain.then(() => { var promises = []; for(var id in obj.user[chat]) { promises.push(new Promise((resolve) => { checkUser(chat, id, resolve); })); } return Promise.all(promises); })); }, Promise.resolve()); 
  • not all obj.user elements are processed anyway. After the first obj.user element returns all obj.user [chat] elements, the script will stop working - teamspam
  • @teamspam, in this case, it is obvious that one of the requests is interrupted to find out which, add to the method, the second handler. which will be executed in case of an error. - Grundy