0

Ok, this is seriously making me lose my mind...

Promise.all([
    one(code),
    two(code),
    three(code),
    four(code),
    five(code),
    six(code),
    seven(code) // <----- This one
]).then(data => {
    // ...
}

The functions are nothing special...

function seven() {
    return new Promise(function (resolve, reject) {
        fetch(`/api-route`) 
            .then(res => res.json())
            .then(data => {
                if (data) {
                    resolve(data);
                } else {
                    reject({
                        status: "Retrieval failed."
                    });
                }
            });
    });
}

If I change the places of six() and seven(), then seven() works and six() no longer does... It makes no sense...

Calling the API routes manually works, but in the promise.all, they can't work after another? What am I missing?

Ivan
  • 1,967
  • 4
  • 34
  • 60
  • Use async-await for in order execution – Kunal Mukherjee Apr 15 '20 at 17:24
  • When some promise within your array failes, the returned promise from `Promise.all` will fail as well. This could cause this "issue". – WolverinDEV Apr 15 '20 at 17:25
  • 1
    I realized that the problem was not in the `promise.all` but rather the database connection i.e. `Global connection already exists. Call sql.close() first.`, preventing it to make the second call. My bad... – Ivan Apr 15 '20 at 17:43
  • 1
    Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Apr 15 '20 at 17:48

0 Answers0