Last active
January 20, 2018 16:09
-
-
Save davidbarral/2b311d195802df42006704ef254211b0 to your computer and use it in GitHub Desktop.
Promisify: node custom 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 } from "util"; | |
const evenSuccess = (n, onSuccess, onError) => { | |
if (n % 2) { | |
onError(n); | |
} else { | |
onSuccess(n); | |
} | |
}; | |
evenSuccess[promisify.custom] = n => new Promise((resolve, reject) => { | |
evenSuccess(n, () => resolve("even"), () => reject("odd")); | |
}); | |
promisify(evenSuccess)(2).then(console.log) // "even" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment