Created
July 13, 2017 03:57
-
-
Save ptgamr/8c40badd70d87b5645e62daa2294c217 to your computer and use it in GitHub Desktop.
Promisify
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 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