Skip to content

Instantly share code, notes, and snippets.

@davidbarral
Last active January 20, 2018 16:09
Show Gist options
  • Save davidbarral/2b311d195802df42006704ef254211b0 to your computer and use it in GitHub Desktop.
Save davidbarral/2b311d195802df42006704ef254211b0 to your computer and use it in GitHub Desktop.
Promisify: node custom promisify
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