Created
November 23, 2016 14:56
-
-
Save riston/f7ddb3a58698928b4eb6be3e92a58189 to your computer and use it in GitHub Desktop.
Promisify function, could not work executed in wrong context(this)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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