V
V
Vitaly2016-12-24 13:07:01
Node.js
Vitaly, 2016-12-24 13:07:01

How to use Bluebird?

1. What is the advantage of Bluebird over Promise?
2. How does promisifyAll determine which features to promisify? Does it override All functions? Does it do the same with nested exports? What happens to synchronous functions like fs.readFileSync?
3. How to use promisifyAll in TypeScript?
4. Are there better alternatives for promissification? For example, a promisified version of the node.js standard libraries

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
yogurt1, 2016-12-24
@yogurt1

1. Very simple
2. promisify takes a `fn` function and returns a function that internally returns a promise, which internally calls `fn` with `arguments` + a function that receives another `arguments` and looks if arguments[0] ( err) is not null, then reject the promise with this err, otherwise resolve the promise with the arguments.
It looks like this:

const promisify = fn => (...args) => new Promise((resolve, reject) => {
  fn(...args, (err, ...nextargs) => {
    if (err) return reject(err)
    return resolve(...nextargs)
  })
})

The promisify code looks a little different and there are all sorts of different checks, but what I gave is a good example of what promisify looks like.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question