Promise.promisify( function(any arguments..., function callback) nodeFunction, [Object { multiArgs: boolean=false, context: any=this } options] ) -> function
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. The node function should conform to node.js convention of accepting a callback as last argument and calling that callback with error as the first argument and success value on the second argument.
If the nodeFunction
calls its callback with multiple success values, the fulfillment value will be the first fulfillment item.
Setting multiArgs
to true
means the resulting promise will always fulfill with an array of the callback's success value(s). This is needed because promises only support a single success value while some callback API's have multiple success value. The default is to ignore all but the first success value of a callback function.
If you pass a context
, the nodeFunction
will be called as a method on the context
.
Example of promisifying the asynchronous readFile
of node.js fs
-module:
var readFile = Promise.promisify(require("fs").readFile); readFile("myfile.js", "utf8").then(function(contents) { return eval(contents); }).then(function(result) { console.log("The result of evaluating myfile.js", result); }).catch(SyntaxError, function(e) { console.log("File had syntax error", e); //Catch any other error }).catch(function(e) { console.log("Error reading file", e); });
Note that if the node function is a method of some object, you can pass the object as the second argument like so:
var redisGet = Promise.promisify(redisClient.get, {context: redisClient}); redisGet('foo').then(function() { //... });
But this will also work:
var getAsync = Promise.promisify(redisClient.get); getAsync.call(redisClient, 'foo').then(function() { //... });
© 2013–2018 Petka Antonov
Licensed under the MIT License.
http://bluebirdjs.com/docs/api/promise.promisify.html