Skip to content

Instantly share code, notes, and snippets.

@scottcorgan
Forked from anonymous/QWrapper.js
Created December 18, 2012 20:07
Show Gist options
  • Save scottcorgan/4331467 to your computer and use it in GitHub Desktop.
Save scottcorgan/4331467 to your computer and use it in GitHub Desktop.
Function wrapper to avoid creating a new deferred in every function using the Q (http://documentup.com/kriskowal/q) library.
/*
Function wrapper to avoid creating a new deferred in every function
using the Q (http://documentup.com/kriskowal/q) library.
*/
// Wrapper Function
function _q (callback) {
return function () {
var deferred = Q.defer(),
args = [].slice.call(arguments, 0);
args.unshift(deferred);
callback.apply(null, args);
return deferred.promise;
}
};
// Usage
var foo = _q(function (_d, arg1, arg2) {
console.log('called function foo');
_d.resolve('foo resolved');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment