I want to work with promises but I have a callback API in a format like:
window.onload; // set to callback
...
window.onload = function() {
};
function request(onChangeHandler) {
...
}
request(function() {
// change happened
...
});
function getStuff(dat, callback) {
...
}
getStuff("dataParam", function(err, data) {
...
})
API;
API.one(function(err, data) {
API.two(function(err, data2) {
API.three(function(err, data3) {
...
});
});
});
This question is related to
javascript
node.js
callback
promise
bluebird
Below is the implementation of how a function (callback API) can be converted to a promise.
function promisify(functionToExec) {
return function() {
var array = Object.values(arguments);
return new Promise((resolve, reject) => {
array.push(resolve)
try {
functionToExec.apply(null, array);
} catch (error) {
reject(error)
}
})
}
}
// USE SCENARIO
function apiFunction (path, callback) { // Not a promise
// Logic
}
var promisedFunction = promisify(apiFunction);
promisedFunction('path').then(()=>{
// Receive the result here (callback)
})
// Or use it with await like this
let result = await promisedFunction('path');
// given you've defined this `Future` fn somewhere:
const Future = fn => {return new Promise((r,t) => fn(r,t))}
// define an eventFn that takes a promise `resolver`
const eventFn = resolve => {
// do event related closure actions here. When finally done, call `resolve()`
something.oneventfired = e => {resolve(e)}
}
// invoke eventFn in an `async` workflowFn using `Future`
// to obtain a `promise` wrapper
const workflowFn = async () => {await Future(eventFn)}
Especially for things like
indexedDb
event wrappers to simplify usage.
Or you might find this variation of Future
to be more general purpose
class PromiseEx extends Promise {
resolve(v,...a) {
this.settled = true; this.settledValue = v;
return(this.resolve_(v,...a))
}
reject(v,...a) {
this.settled = false; this.settledValue = v;
return(this.reject_(v,...a))
}
static Future(fn,...args) {
let r,t,ft = new PromiseEx((r_,t_) => {r=r_;t=t_})
ft.resolve_ = r; ft.reject_ = t; fn(ft,...args);
return(ft)
}
}
When you have a few functions that take a callback and you want them to return a promise instead you can use this function to do the conversion.
function callbackToPromise(func){
return function(){
// change this to use what ever promise lib you are using
// In this case i'm using angular $q that I exposed on a util module
var defered = util.$q.defer();
var cb = (val) => {
defered.resolve(val);
}
var args = Array.prototype.slice.call(arguments);
args.push(cb);
func.apply(this, args);
return defered.promise;
}
}
I don't think the window.onload
suggestion by @Benjamin will work all the time, as it doesn't detect whether it is called after the load. I have been bitten by that many times. Here is a version which should always work:
function promiseDOMready() {
return new Promise(function(resolve) {
if (document.readyState === "complete") return resolve();
document.addEventListener("DOMContentLoaded", resolve);
});
}
promiseDOMready().then(initOnLoad);
It is like 5 years late, but I wanted to post here my promesify version which takes functions from callbacks API and turns them into promises
const promesify = fn => {
return (...params) => ({
then: cbThen => ({
catch: cbCatch => {
fn(...params, cbThen, cbCatch);
}
})
});
};
Take a look to this very simple version here: https://gist.github.com/jdtorregrosas/aeee96dd07558a5d18db1ff02f31e21a
Today, I can use Promise
in Node.js
as a plain Javascript method.
A simple and basic example to Promise
(with KISS way):
Plain Javascript Async API code:
function divisionAPI (number, divider, successCallback, errorCallback) {
if (divider == 0) {
return errorCallback( new Error("Division by zero") )
}
successCallback( number / divider )
}
Promise
Javascript Async API code:
function divisionAPI (number, divider) {
return new Promise(function (fulfilled, rejected) {
if (divider == 0) {
return rejected( new Error("Division by zero") )
}
fulfilled( number / divider )
})
}
(I recommend visiting this beautiful source)
Also Promise
can be used with together async\await
in ES7
to make the program flow wait for a fullfiled
result like the following:
function getName () {
return new Promise(function (fulfilled, rejected) {
var name = "John Doe";
// wait 3000 milliseconds before calling fulfilled() method
setTimeout (
function() {
fulfilled( name )
},
3000
)
})
}
async function foo () {
var name = await getName(); // awaits for a fulfilled result!
console.log(name); // the console writes "John Doe" after 3000 milliseconds
}
foo() // calling the foo() method to run the code
Another usage with the same code by using .then()
method
function getName () {
return new Promise(function (fulfilled, rejected) {
var name = "John Doe";
// wait 3000 milliseconds before calling fulfilled() method
setTimeout (
function() {
fulfilled( name )
},
3000
)
})
}
// the console writes "John Doe" after 3000 milliseconds
getName().then(function(name){ console.log(name) })
Promise
can also be used on any platform that is based on Node.js like react-native
.
Bonus: An hybrid method
(The callback method is assumed to have two parameters as error and result)
function divisionAPI (number, divider, callback) {
return new Promise(function (fulfilled, rejected) {
if (divider == 0) {
let error = new Error("Division by zero")
callback && callback( error )
return rejected( error )
}
let result = number / divider
callback && callback( null, result )
fulfilled( result )
})
}
The above method can respond result for old fashion callback and Promise usages.
Hope this helps.
In Node.js 8 you can promisify object methods on the fly using this npm module:
https://www.npmjs.com/package/doasync
It uses util.promisify and Proxies so that your objects stay unchanged. Memoization is also done with the use of WeakMaps). Here are some examples:
With objects:
const fs = require('fs');
const doAsync = require('doasync');
doAsync(fs).readFile('package.json', 'utf8')
.then(result => {
console.dir(JSON.parse(result), {colors: true});
});
With functions:
doAsync(request)('http://www.google.com')
.then(({body}) => {
console.log(body);
// ...
});
You can even use native call
and apply
to bind some context:
doAsync(myFunc).apply(context, params)
.then(result => { /*...*/ });
Before converting a function as promise In Node.JS
var request = require('request'); //http wrapped module
function requestWrapper(url, callback) {
request.get(url, function (err, response) {
if (err) {
callback(err);
}else{
callback(null, response);
}
})
}
requestWrapper(url, function (err, response) {
console.log(err, response)
})
After Converting It
var request = require('request');
function requestWrapper(url) {
return new Promise(function (resolve, reject) { //returning promise
request.get(url, function (err, response) {
if (err) {
reject(err); //promise reject
}else{
resolve(response); //promise resolve
}
})
})
}
requestWrapper('http://localhost:8080/promise_request/1').then(function(response){
console.log(response) //resolve callback(success)
}).catch(function(error){
console.log(error) //reject callback(failure)
})
Incase you need to handle multiple request
var allRequests = [];
allRequests.push(requestWrapper('http://localhost:8080/promise_request/1'))
allRequests.push(requestWrapper('http://localhost:8080/promise_request/2'))
allRequests.push(requestWrapper('http://localhost:8080/promise_request/5'))
Promise.all(allRequests).then(function (results) {
console.log(results);//result will be array which contains each promise response
}).catch(function (err) {
console.log(err)
});
You can do something like this
// @flow
const toPromise = (f: (any) => void) => {
return new Promise<any>((resolve, reject) => {
try {
f((result) => {
resolve(result)
})
} catch (e) {
reject(e)
}
})
}
export default toPromise
Then use it
async loadData() {
const friends = await toPromise(FriendsManager.loadFriends)
console.log(friends)
}
Under node v7.6+ which has built in promises and async:
// promisify.js
let promisify = fn => (...args) =>
new Promise((resolve, reject) =>
fn(...args, (err, result) => {
if (err) return reject(err);
return resolve(result);
})
);
module.exports = promisify;
How to use:
let readdir = require('fs').readdir;
let promisify = require('./promisify');
let readdirP = promisify(readdir);
async function myAsyncFn(path) {
let entries = await readdirP(path);
return entries;
}
With plain old vanilla javaScript, here's a solution to promisify an api callback.
function get(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('get', url);
xhr.addEventListener('readystatechange', function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log('successful ... should call callback ... ');
callback(null, JSON.parse(xhr.responseText));
} else {
console.log('error ... callback with error data ... ');
callback(xhr, null);
}
}
});
xhr.send();
}
/**
* @function promisify: convert api based callbacks to promises
* @description takes in a factory function and promisifies it
* @params {function} input function to promisify
* @params {array} an array of inputs to the function to be promisified
* @return {function} promisified function
* */
function promisify(fn) {
return function () {
var args = Array.prototype.slice.call(arguments);
return new Promise(function(resolve, reject) {
fn.apply(null, args.concat(function (err, result) {
if (err) reject(err);
else resolve(result);
}));
});
}
}
var get_promisified = promisify(get);
var promise = get_promisified('some_url');
promise.then(function (data) {
// corresponds to the resolve function
console.log('successful operation: ', data);
}, function (error) {
console.log(error);
});
es6-promisify
converts callback-based functions to Promise-based functions.
const promisify = require('es6-promisify');
const promisedFn = promisify(callbackedFn, args);
The Q library by kriskowal includes callback-to-promise functions. A method like this:
obj.prototype.dosomething(params, cb) {
...blah blah...
cb(error, results);
}
can be converted with Q.ninvoke
Q.ninvoke(obj,"dosomething",params).
then(function(results) {
});
My promisify version of a callback
function is the P
function:
var P = function() {_x000D_
var self = this;_x000D_
var method = arguments[0];_x000D_
var params = Array.prototype.slice.call(arguments, 1);_x000D_
return new Promise((resolve, reject) => {_x000D_
if (method && typeof(method) == 'function') {_x000D_
params.push(function(err, state) {_x000D_
if (!err) return resolve(state)_x000D_
else return reject(err);_x000D_
});_x000D_
method.apply(self, params);_x000D_
} else return reject(new Error('not a function'));_x000D_
});_x000D_
}_x000D_
var callback = function(par, callback) {_x000D_
var rnd = Math.floor(Math.random() * 2) + 1;_x000D_
return rnd > 1 ? callback(null, par) : callback(new Error("trap"));_x000D_
}_x000D_
_x000D_
callback("callback", (err, state) => err ? console.error(err) : console.log(state))_x000D_
callback("callback", (err, state) => err ? console.error(err) : console.log(state))_x000D_
callback("callback", (err, state) => err ? console.error(err) : console.log(state))_x000D_
callback("callback", (err, state) => err ? console.error(err) : console.log(state))_x000D_
_x000D_
P(callback, "promise").then(v => console.log(v)).catch(e => console.error(e))_x000D_
P(callback, "promise").then(v => console.log(v)).catch(e => console.error(e))_x000D_
P(callback, "promise").then(v => console.log(v)).catch(e => console.error(e))_x000D_
P(callback, "promise").then(v => console.log(v)).catch(e => console.error(e))
_x000D_
The P
function requires that the callback signature must be callback(error,result)
.
The callback style function always like this(almost all function in node.js is this style):
//fs.readdir(path[, options], callback)
fs.readdir('mypath',(err,files)=>console.log(files))
This style has same feature:
the callback function is passed by last argument.
the callback function always accept the error object as it's first argument.
So, you could write a function for convert a function with this style like this:
const R =require('ramda')
/**
* A convenient function for handle error in callback function.
* Accept two function res(resolve) and rej(reject) ,
* return a wrap function that accept a list arguments,
* the first argument as error, if error is null,
* the res function will call,else the rej function.
* @param {function} res the function which will call when no error throw
* @param {function} rej the function which will call when error occur
* @return {function} return a function that accept a list arguments,
* the first argument as error, if error is null, the res function
* will call,else the rej function
**/
const checkErr = (res, rej) => (err, ...data) => R.ifElse(
R.propEq('err', null),
R.compose(
res,
R.prop('data')
),
R.compose(
rej,
R.prop('err')
)
)({err, data})
/**
* wrap the callback style function to Promise style function,
* the callback style function must restrict by convention:
* 1. the function must put the callback function where the last of arguments,
* such as (arg1,arg2,arg3,arg...,callback)
* 2. the callback function must call as callback(err,arg1,arg2,arg...)
* @param {function} fun the callback style function to transform
* @return {function} return the new function that will return a Promise,
* while the origin function throw a error, the Promise will be Promise.reject(error),
* while the origin function work fine, the Promise will be Promise.resolve(args: array),
* the args is which callback function accept
* */
const toPromise = (fun) => (...args) => new Promise(
(res, rej) => R.apply(
fun,
R.append(
checkErr(res, rej),
args
)
)
)
For more concise, above example used ramda.js. Ramda.js is a excellent library for functional programming. In above code, we used it's apply(like javascript function.prototype.apply
) and append(like javascript function.prototype.push
).
So, we could convert the a callback style function to promise style function now:
const {readdir} = require('fs')
const readdirP = toPromise(readdir)
readdir(Path)
.then(
(files) => console.log(files),
(err) => console.log(err)
)
toPromise and checkErr function is own by berserk library, it's a functional programming library fork by ramda.js(create by me).
Hope this answer is useful for you.
You can use native Promise in ES6, for exemple dealing with setTimeout:
enqueue(data) {
const queue = this;
// returns the Promise
return new Promise(function (resolve, reject) {
setTimeout(()=> {
queue.source.push(data);
resolve(queue); //call native resolve when finish
}
, 10); // resolve() will be called in 10 ms
});
}
In this exemple, the Promise has no reason to fail, so reject()
is never called.
You can use JavaScript native promises with Node JS.
My Cloud 9 code link: https://ide.c9.io/adx2803/native-promises-in-node
/**
* Created by dixit-lab on 20/6/16.
*/
var express = require('express');
var request = require('request'); //Simplified HTTP request client.
var app = express();
function promisify(url) {
return new Promise(function (resolve, reject) {
request.get(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
resolve(body);
}
else {
reject(error);
}
})
});
}
//get all the albums of a user who have posted post 100
app.get('/listAlbums', function (req, res) {
//get the post with post id 100
promisify('http://jsonplaceholder.typicode.com/posts/100').then(function (result) {
var obj = JSON.parse(result);
return promisify('http://jsonplaceholder.typicode.com/users/' + obj.userId + '/albums')
})
.catch(function (e) {
console.log(e);
})
.then(function (result) {
res.end(result);
})
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
//run webservice on browser : http://localhost:8081/listAlbums
From the future
A simple generic function I normally use.
const promisify = (fn, ...args) => {
return new Promise((resolve, reject) => {
fn(...args, (err, data) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
How to use it
promisify(fn, arg1, arg2)
You are probably not looking to this answer, but this will help understand the inner workings of the available utils
In release candidate for Node.js 8.0.0, there's a new utility, util.promisify
(I've written about util.promisify), that encapsulates the capacity of promisifying whatever function.
It is not much different from the approaches suggested in the other answers, but has the advantage of being a core method, and not requiring additional dependencies.
const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
Then you've a readFile
method that returns a native Promise
.
readFile('./notes.txt')
.then(txt => console.log(txt))
.catch(...);
Node.js 8.0.0 includes a new util.promisify()
API that allows standard Node.js callback style APIs to be wrapped in a function that returns a Promise. An example use of util.promisify()
is shown below.
const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
readFile('/some/file')
.then((data) => { /** ... **/ })
.catch((err) => { /** ... **/ });
Source: Stackoverflow.com