NPM cool Library: Q enhanced promise

Time:2020-10-27

NPM cool library, two minutes a day, learn about a popular NPM library.

Yesterday we learned how to use promise to manage asynchronous tasks, but the es2015 standard promise object implementation interface is very limited, only containsPromise.all() Promise.race() promise.then() promise.catch()For example, we can’t directly judge whether a promise object has been completed.

q

Q is a promise implementation library, and compared with the es2015 standard, Q provides many practical tool functions. Here are some very useful functions:

Judge promise status

  • promise.isFulfilled()
  • promise.isRejected()
  • promise.isPending()

promise.finally(callback)

Promise is called after completion, regardless of whether or not it fails. Note that ES2016 standard Promise has this function, but some browsers such as Edge do not implement this function.

promise.timeout(ms,message)

Asynchronous processing timeout setting

promise.delay(ms)

Delay execution

Other utility functions about return value

such aspromise.get(propertyName)Is used to obtain a property of promise returned object, which is equivalent to:

promise.then(function (o) {
    return o[propertyName];
});

There are many such functions. You can check the API documentation for details.

reference material

https://github.com/kriskowal/q
https://github.com/kriskowal/…