Skip to content

Instantly share code, notes, and snippets.

@riston
Created November 23, 2016 14:56
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 riston/f7ddb3a58698928b4eb6be3e92a58189 to your computer and use it in GitHub Desktop.
Save riston/f7ddb3a58698928b4eb6be3e92a58189 to your computer and use it in GitHub Desktop.
Promisify function, could not work executed in wrong context(this)
const _promiseFn = fn => {
return function promisified(...args) {
const self = this; // promisification if performance critical
return new Promise((resolve, reject) => {
const cbFn = (error, result) => {
if (error) {
return reject(new Error(error));
}
if (result && false === result.success) {
return reject(new Error(result.message));
}
return resolve(result);
};
args.push(cbFn);
fn.apply(self, args); // call with arguments
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment