承诺所有返回错误太多请求



>我正在使用全部承诺发送多个承诺,但出现此错误

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

我有数据列表,我按 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)

}

我按 10 组对数据进行分块

但是您仍然同时请求所有块。因此,分块的意义不大。在处理下一个块之前,您应该使用循环并await每个块,而不是使用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 来说仍然太快,您可能会延迟循环

最新更新