I’m working on a script to seed my database but am having issues with my node-pg connection being terminated before my rebuild function is finished.
const rebuildDB = async () => {
try {
client.connect();
await dropTables();
await createTables();
await Promise.all(employees.map(createEmployee), customers.map(createCustomer));
} catch (err) {
console.log(err);
} finally {
client.end();
}
};
rebuildDB();
When I remove the finally block everything runs correctly. If i leave it, the createEmployee function will execute but the connection will be terminated before createCustomer() is able to execute.
Any suggestions?
2
Answers
You can concat your two promises arrays in order to have the one expected iterable argument
This is equivalent in terms of result with the spread operator given in other answers. However if your arrays are very large (and I assume they could be if they are all the entries from a db) the spread operator could throw a
Maximum call stack size exceeded
error. Concat is more efficient on large arrays, especially if you only have two arrays to merge.Read more about this on https://www.educative.io/answers/spread-operator-vs-arrayprototypeconcat-in-javascript
Reposting my comment as a proper answer (and adding to it):
There are two issues. The main one, which you asked about, is that you’re calling
Promise.all
incorrectly. It accepts only a single argument, not multiple arguments, so it’s ignoring the promises from the second argument entirely. You want to pass in a single iterable (probably an array in this case).The other problem is that you don’t want
client.connect()
inside thetry
, since you don’t want to callclient.end()
ifclient.connect()
throws.So: