Saturday 13 May 2017

How to add a short Timeout before each request-promise api call in a loop

So I am new to Promise, I want to use Promise to do multiple url call. Now it seems that the algorithm works but the problem is that the riot api has a rate limit, so I have to let the program wait for a short time after each api call. How should I add this?

var matchidList = [];
                for (var i = 0; i < pml.length; i++) {
                  matchidList.push(pml[i]["gameId"]);
                }

                var matchuri = "https://na1.api.riotgames.com/lol/match/v3/matches/";
                var uris = [];
                for (var i = 0; i < matchidList.length; i++) {
                  uris.push(matchuri + matchidList[i] + "?" + apikey);
                }

                Promise.map(uris, function(url) {
                  return rp.getAsync(url).spread(function(response,body) {
                          return JSON.parse(body);
                  });
                }).then(function(results) {
                     // results is an array of all the parsed bodies in order
                     console.log(results);
                     tag.checkTags("").then((tags) =>{
                       res.render('index', { result: body,
                                             val: search,
                                             matches: ''
                                           });
                     });
                }).catch(function(err) {
                    console.log(err);
                     // handle error here
                });



via Heqian Liu

No comments:

Post a Comment