我使用 Promise all 发送多个 Promise,但收到此错误
429 - {"error":{"code":"TooManyRequests","message":"Too many requests"}}
我有数据列表,我将数据按 10 组进行分块,然后为每个数据发送通知
await Promise.all(usersList.map(usersTokens=> {
return sendPush(heading, content,usersTokens, platform).catch((e) => {
console.error(e)
errors.push({ e, android })
})
}))
发送推送功能
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)
}
最佳答案
I chunk data by group of 10
但是您仍然同时请求所有 block 。因此分块的意义不大。您应该使用循环并在处理下一个 block 之前await
每个 block ,而不是使用Promise.all
:
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 })
}
}
如果这对于 API 来说仍然太快,您可以 delay the loop
关于javascript - promise 所有返回错误太多请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51185243/