Javascript 承诺所有返回错误太多请求
我正在使用promise all来发送多个承诺,我收到了这个错误 429-{“错误”:{“代码”:“TooManyRequests”,“消息”:“太多” 请求“}} 我有一个数据列表,我按10人一组对数据进行分块,然后我为每个人发送通知Javascript 承诺所有返回错误太多请求,javascript,node.js,promise,request,request-promise,Javascript,Node.js,Promise,Request,Request Promise,我正在使用promise all来发送多个承诺,我收到了这个错误 429-{“错误”:{“代码”:“TooManyRequests”,“消息”:“太多” 请求“}} 我有一个数据列表,我按10人一组对数据进行分块,然后我为每个人发送通知 await Promise.all(usersList.map(usersTokens=> { return sendPush(heading, content,usersTokens, platform).ca
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人一组分块
但是您仍然同时请求所有块。因此,分块没有什么意义。不要使用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来说仍然太快,您可以错误消息很可能来自您正在使用的web服务。您应该降低每分钟的请求速率。这意味着增加延迟。每分钟对我来说太多了。因为这是给人们发送通知。我不是说每分钟一次。我说的是速度,就像你说一辆车应该以120公里/小时的速度行驶,但这并不意味着它必须行驶一小时。:-)@t、 j.crowder我同意这是一个骗局,但如果在nodejs上运行,答案有点过时/过于复杂。为什么要将所有承诺都推到结果数组中?我以前尝试过使用for,但有时我从服务器错误“timeout”中得到,可能在for循环中每个承诺都需要更多的时间?@adir它确实推送了承诺结果,如果API没有返回有意义的内容,则不需要这样做。超时意味着您应该稍后重试该操作是的,但当它显示超时时,我正在等待推送的答复,因此系统崩溃。但我会按您的方式尝试,