Close database connection after callback call

Just not sure about the flow of this code

doStuff()
  .then(() => { callback(); })
  .catch(err => callback(err))
  .then(db.close);

      

Will the db.close call be called in this script?

+3


source to share


1 answer


Yes, he will always be called, assuming that callback(err)

he does not, he throws himself.

This code is almost equivalent (slightly different from error handling) and a little more concise:



doStuff()
    .then(callback, callback)
    .then(db.close)

      

Another point worth mentioning is that if it db

is an instance of a class and close()

relies on it with a keyword this

, then passing it in this way can be problematic as it loses that context when on its own.

+2


source







All Articles