1

I'm using promise all for send multiple promises and I get this error

429 - {"error":{"code":"TooManyRequests","message":"Too many requests"}}

I have list of data and I chunk data by group of 10 and then I send for each of them notification

 await Promise.all(usersList.map(usersTokens=> {

                    return sendPush(heading, content,usersTokens, platform).catch((e) => {
                        console.error(e)
                        errors.push({ e, android })
                    })
                }))

send push function

import * as rp from 'request-promise'
export const sendPush = (title="",secondTitle,tokens,platform) => {


let message = {
    notification_content : {
        name:title,
        title : secondTitle,
        body : secondTitle,

     },
       notification_target : {
       type : "devices_target",
       devices : tokens
     },
   }

 var headers = {
    "Content-Type": "application/json; charset=utf-8",
    "X-API-Token": 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
};

var options = {
    uri: `https://api.appcenter.ms/v0.1/apps/XXXXXXXXXX/${platform}/push/notifications`,
    method: "POST",
    headers: headers,
    body: message,
    json: true
}

  return rp(options)

}

Manspof
  • 598
  • 26
  • 81
  • 173
  • The error message is most probably coming from the web service you are using. You should lower the rate of requests per minute. This means adding delays. – trincot Jul 05 '18 at 07:10
  • per minute it's too much for me..because it's to send notification for people – Manspof Jul 05 '18 at 07:11
  • I am not saying one per minute. I am talking of a *rate*, like when you say a car should drive 120 km / hour, it does not mean it has to drive for one hour. :-) – trincot Jul 05 '18 at 07:12
  • @t.j.crowder I agree that this is a dupe, but the answer is a bit outdated / overcomplicated if running on nodejs. – Jonas Wilms Jul 05 '18 at 07:15

1 Answers1

1

I chunk data by group of 10

But you still request all the chunks at the same time. Therefore chunking makes less sense. Instead of using Promise.all you should use a loop and await each chunk before processing the next one:

 const result = [];

 for(const userTokens of userList) {
   try {
     result.push(await sendPush(heading, content,usersTokens, platform));
   } catch(e) {
     console.error(e)
     errors.push({ e, android })
  }
}

If that is still too fast for the API, you could delay the loop

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • why to push all promise into result array? I tried before to use in for but somtimes I get from server error of 'timeout' maybe in for loop it takes more time each promise? – Manspof Jul 05 '18 at 07:18
  • @adir it does push the Promises results, you don't need that if the API does not return something meaningful. And a timeout means you should retry the operation a bit later – Jonas Wilms Jul 05 '18 at 07:20
  • yes but when it shows me timeout and I'm waiting for answer from the push so the system is crash. – Manspof Jul 05 '18 at 07:23
  • but I will try your way, – Manspof Jul 05 '18 at 07:24