Dancrumb Dancrumb - 4 months ago 15
Node.js Question

Why might a node app using Promises not return?

I'm using Bluebird in a CLI app.

For some reason, the app is not completing, but I can't fathom why. I've tried running in debug (in Webstorm) and pausing while it's "hung" but I get no information.

The code is of the form:

Promise.all([ /* promises */ ])
.then(function () {
return Promise.all([ /* promises */ ]);
})

.then(function () {
return console.log("Done");
})
.catch(function (err) {
return console.error("Failed: " + err);
});


The app reports
Done
and, by inspecting my database (because the promises are from SQL INSERTs), I can see everything in place.

I'm using

mysql.createPool();


from
promise-mysql
to send my queries.

How can I debug this hang? Or are there common failure modes that I could explore?

Answer

It sounds like you may not be closing all of your mysql connections or not terminating the pool. If you're not already doing so, it would be wise to take care of that in a .finally() after your mysql operations:

pool.end();

As indicated in the docs (emphasis added):

When you are done using the pool, you have to end all the connections or the Node.js event loop will stay active until the connections are closed by the MySQL server. This is typically done if the pool is used in a script or when trying to gracefully shutdown a server. To end all the connections in the pool, use the end method on the pool