Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How promisifyAll works, or what are the requirements for it work?

In a promise library bluebird have function promisifyAll or other similar libraries that claim to convert async functions with callback patterns into promise based ie. resolve(), reject(), or done()..So how does it work?

For example:

function myAsync1 (data, url, callBack) {...}

and if i put it in

Promise.promisify(myAsycn1);

then will my function work like this..

myAsync1('{..}', 'http://..').then(function(){...});

This is have been bothering me. Is there a pattern that async non promise libs or function need to follow for Bluebird promisifyAll to convert them to promises based methods or there is some magic that converts them.

If not then what are the requirements and how does it work with existing libraries like mongodb etc.

like image 237
Muhammad Umer Avatar asked Apr 12 '15 22:04

Muhammad Umer


People also ask

What is Promisifyall?

Promisifies the entire object by going through the object's properties and creating an async equivalent of each function on the object and its prototype chain. The promisified method name will be the original method name suffixed with suffix (default is "Async" ).

Why do we need to Promisify?

Promisification helps in dealing with callback-based APIs while keeping code consistent with promises. We could just wrap any function with new Promise() and not worry about it at all. But doing that when we have many functions would be redundant.

What does promise Promisify do?

promisify. Returns a function that will wrap the given nodeFunction . Instead of taking a callback, the returned function will return a promise whose fate is decided by the callback behavior of the given node function.

How do you Promisify a callback function?

To convert a callback into a promise, you need to return a promise. You run the code with the callback inside the promise. const readFilePromise = () => { return new Promise((resolve, reject) => { fs. readFile(filePath, options, (err, data) => { // ... }) }) }


1 Answers

Is there a pattern that async non promise libs or function need to follow for Bluebird promisifyAll to convert them to promises based methods

Yes, there is a pattern. The functions it converts must expect a callback as their last argument. Additionally, it must pass an error as the first argument to the callback (null if no error) and the return value as the second argument.

The BlueBird promisify function is very difficult to follow because of optimizations, so I'll show a simple way it could be written:

function promisify(fn) {
  return function() {
    var that = this; // save context
    var args = slice.call(arguments); // turn into real array
    return new Promise(function(resolve, reject) {
      var callback = function(err, ret) { // here we assume the arguments to
                                          // the callback follow node.js
                                          // conventions
        if(err != undefined) {
          reject(err);
        } else {
          resolve(ret);
        }
      };
      fn.apply(that, args.concat([callback])); // Now assume that the last argument will
                                              // be used as a callback
    });
  };
}

Now we could implement promisifyAll by looping over the functions in the target object and using promisify on each one.

like image 110
Aaron Dufour Avatar answered Sep 23 '22 13:09

Aaron Dufour