Skip to content

Instantly share code, notes, and snippets.

@ptgamr
Created July 13, 2017 03:57
Show Gist options
  • Save ptgamr/8c40badd70d87b5645e62daa2294c217 to your computer and use it in GitHub Desktop.
Save ptgamr/8c40badd70d87b5645e62daa2294c217 to your computer and use it in GitHub Desktop.
Promisify
const promisify = func => {
// this need to not using arrow function, otherwise the `this` context will be wrong
return function() {
const args = Array.from(arguments);
return new Promise((resolve, reject) => {
const callback = (err, data) => {
if (error) {
return reject(err);
}
return resolve(data);
};
args.push(callback);
func.apply(this, args);
});
};
}
API.someAsyncFunc = promisify(someAsyncFunc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment