Sign in
Log inSign up

How does library q's defer works in JavaScript?

Ankur Anand's photo
Ankur Anand
·Jan 25, 2018

This is the code written using q library .

update = (selectionCriteria, updateCriteria, updateOptions, options) => {
  let q = Q.defer()
    , db = options.db
    , logger = options.logger

  db.collection(collectionName).update(selectionCriteria, updateCriteria, updateOptions, (err, doc) => {
    if (err) { 
      logger.error(err)
      q.reject(new Error("SOMETHING_WENT_WRONG",options))
    }
    if(doc.result.n > 0)
      q.resolve(true)
    else
      q.resolve(false)
    logger.info("Updating")
  })
  return q.promise
}

What i'm not able to understand is how logger.info is able to execute even after the q.resolve(false) has been done ?

Doesn't q.resolve(false) short-circuit the flow ?