为什么promise.all崩溃错误却没有一一发送promise?
我在这里很新,我的后端nodejs有一个问题。
我有一个对象列表,可以帮助我在数据库中找到一辆汽车,因此我准备的承诺要一一获取数据并将其发送到承诺中。所有这些都可以触发承诺。
函数GetCar每次都可以使用我发送的数据来工作,但是当我做出承诺时。所有数组都会有一个连接池的错误。有什么问题?
function requestData (listOfCar) {
const promiseList = [];
for (let i = 0; i < listOfCar.length; i++) {
promiseList.push(getCar(listOfCar[i]));
}
return Promise.all(promiseList); // crash but sending promises one by one is working
}
function getCar(carFinder) {
// do things and return a query find in sequelize to find a car
// and so it return a promise
}
I'm pretty new here and I have a problem on my backend nodejs.
I have a list of object that will help me find a car in the database, and so I prepare promises to get data one by one and send that inside a promise.all to trigger the promises.
the function getCar is working every time with data I sent but When I do the promise.all with the array then it will have an error of pool of connection. What is the probleme ?
function requestData (listOfCar) {
const promiseList = [];
for (let i = 0; i < listOfCar.length; i++) {
promiseList.push(getCar(listOfCar[i]));
}
return Promise.all(promiseList); // crash but sending promises one by one is working
}
function getCar(carFinder) {
// do things and return a query find in sequelize to find a car
// and so it return a promise
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
承诺总是直接触发的,他们不会等待在承诺内触发。
所以您的问题是您正在发送我的方式,我猜想数据库内的许多请求,因此连接池不再接受
为了解决您可以添加更多连接池的方法,也可以简单地触发Prome Promise 5的小部分并等待承诺。
Promise are always directly trigger, they do not wait to be trigger inside the promise.all.
So your problem is that you are sending I guess way to many request inside the database and so the pool of connection do not accept it anymore
To fix that you can add more pool of connection or you can simply trigger promise little chunk of 5 and await the promise.all