Last active
December 19, 2020 14:37
-
-
Save gm50x/dd57c7b9b1e085dd40beacefaa820d39 to your computer and use it in GitHub Desktop.
Promisified a callback function
This file contains 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 } = require("util"); | |
const getCustomerInfo = (callback) => { | |
setTimeout( | |
() => callback(null, { name: "John Doe", birthdate: "1967-04-14" }), | |
1000 | |
); | |
}; | |
const getCustomerInfoAsync = promisify(getCustomerInfo); | |
const callback = () => { | |
getCustomerInfo((err, customerInfo) => { | |
console.log(customerInfo); | |
}); | |
}; | |
const promisified = async () => { | |
const customerInfo = await getCustomerInfoAsync(); | |
console.log(customerInfo); | |
}; | |
const main = () => { | |
callback(); | |
promisified(); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment