为什么promise.all崩溃错误却没有一一发送promise?

发布于 2025-01-17 13:12:32 字数 588 浏览 3 评论 0原文

我在这里很新,我的后端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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极致的悲 2025-01-24 13:12:32

承诺总是直接触发的,他们不会等待在承诺内触发。
所以您的问题是您正在发送我的方式,我猜想数据库内的许多请求,因此连接池不再接受
为了解决您可以添加更多连接池的方法,也可以简单地触发Prome Promise 5的小部分并等待承诺。

async function requestData (listOfCar) {
    const carLists = [];

    for (let i = 0; i < listOfCar.length; i++) {
        const tmpListOfPromise = [];
        for (let j = 0; j < 5 && i < listOfCar; j++) {
            const tmpListOfPromise.push(getCar(listOfCar[i]));
            i = i + 1;
        }
        await Promise.all(tmpListOfPromise).then((response) => {
            carLists = carLists.concat(response);  // this will push every response in the list
        });
    }
}

function getCar(carFinder) {
    // do things and return a query find in sequelize to find a car
    // and so it return a promise
}

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

async function requestData (listOfCar) {
    const carLists = [];

    for (let i = 0; i < listOfCar.length; i++) {
        const tmpListOfPromise = [];
        for (let j = 0; j < 5 && i < listOfCar; j++) {
            const tmpListOfPromise.push(getCar(listOfCar[i]));
            i = i + 1;
        }
        await Promise.all(tmpListOfPromise).then((response) => {
            carLists = carLists.concat(response);  // this will push every response in the list
        });
    }
}

function getCar(carFinder) {
    // do things and return a query find in sequelize to find a car
    // and so it return a promise
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文