Skip to content

Instantly share code, notes, and snippets.

@briancavalier
Created February 24, 2012 13:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briancavalier/1900788 to your computer and use it in GitHub Desktop.
Save briancavalier/1900788 to your computer and use it in GitHub Desktop.
Turn callback-based functions into promise-based functions
var when = require('../when');
// A test callback-based function
function callbackBased(value, callback, errback) {
setTimeout(function() {
(value ? callback : errback)(value);
}, 100);
}
function log(val) {
console.log('ok', val);
}
function err(val) {
console.log('ERR', val);
}
// Just to show expected behavior
callbackBased(1, log, err);
callbackBased(0, log, err);
// promisify takes a callback-based function and returns an
// equivalent function that returns a promise instead of using callbacks.
function promisify(func, callbackPos, errbackPos, progbackPos) {
var orig, initArgs;
orig = func;
// If you only supply the function, assume callback and errback
// will always be the last two params.
// If you supply positions, use them to inject callback/errback/progback
// into the args.
if(arguments.length === 1) {
initArgs = function(args, deferred) {
args.push(deferred.resolve);
args.push(deferred.reject);
return args;
}
} else {
initArgs = function(args, deferred) {
if(typeof callbackPos == 'number') {
args.splice(callbackPos, 0, deferred.resolve);
}
if(typeof errbackPos == 'number') {
args.splice(errbackPos, 0, deferred.reject);
}
if(typeof progbackPos == 'number') {
args.splice(progbackPos, 0, deferred.progress);
}
return args;
}
}
return function() {
var d = when.defer();
orig.apply(null, initArgs(Array.prototype.slice.call(arguments), d));
return d.promise;
}
}
var promiseBased = promisify(callbackBased);
promiseBased(1).then(log, err);
promiseBased(0).then(log, err);
promiseBased = promisify(callbackBased, 1, 2);
promiseBased(1).then(log, err);
promiseBased(0).then(log, err);
// Negatives work, but are wonky
promiseBased = promisify(callbackBased, -1, -1);
promiseBased(1, 2).then(log, err);
promiseBased(0, 2).then(log, err);
@millermedeiros
Copy link

nice! liked that it's flexible enough to change the order of the callbacks, even being a little bit weird at the 1st look.

@briancavalier
Copy link
Author

Thanks, Miller! I've been playing with some other variations of it, but so far this one is still my favorite. It may be making an appearance in the next rev of when.js :)

@unscriptable
Copy link

This is pretty neat, but I think I'm missing something obvious. My brain (in it's current tired state) thinks the following is much clearer and easier:

var d = when.defer();
callbackBased(1, d.resolve, d.reject);

-- John :)

@briancavalier
Copy link
Author

The difference is that it allows you to create a new promise-based function that other components can consume instead of a callback-based function. I think that has a couple advantages:

  1. It makes it easier, imho, to use a callback-based function in a project where you'd prefer to be using promises. Create the promisified version once, then pass that around instead of the original callback-based version.
  2. Under many circumstances, a component can call the new function as if it's a synchronous function. If the component doesn't actually need to observe the result, they can simply call the new function and return the result (which happens to be a promise). This is along the lines of what I was getting at in my first two Async Programming blog posts (part 1, part 2).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment