Skip to content

Instantly share code, notes, and snippets.

@lilactown
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lilactown/27b287d563a43ce19bab to your computer and use it in GitHub Desktop.
Save lilactown/27b287d563a43ce19bab to your computer and use it in GitHub Desktop.
ES6 denodeify
const denodeify = (fn) =>
function (...args) {
// bind our node function to the current context with arguments
const boundFn = fn.bind(this, ...args);
return new Promise((resolve, reject) => {
// call our bound node-style function
// typical node-style is `callback (err, result)`
// but we can account for more arguments
// by using a spread
boundFn((err, ...results) =>
// if an err, reject, otherwise resolve with results
(err) ? reject (err) : resolve (...results)
);
});
};
// Short version:
const denodeify = (fn) => function (...args) {
const boundFn = fn.bind(this, ...args);
return new Promise((resolve, reject) => {
boundFn((err, ...results) => (err) ? reject (err) : resolve (...results) );
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment