Skip to content

Instantly share code, notes, and snippets.

@wearhere
Created March 21, 2017 18:06
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 wearhere/5f9660fa5af8793da7a64bbf70ea1bbc to your computer and use it in GitHub Desktop.
Save wearhere/5f9660fa5af8793da7a64bbf70ea1bbc to your computer and use it in GitHub Desktop.
Simple promisify.
function promisify(fn) {
return function(...args) {
return new Promise((resolve, reject) => {
fn.call(this, ...args, (err, res) => {
if (err) reject(err);
else resolve(res);
});
});
};
};
function respond(subject, done) {
setTimeout(() => done(null, `hello ${subject}`), 2000);
}
let respondWithPromise = promisify(respond);
respondWithPromise('world').then((val) => console.log(val));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment