Skip to content

Instantly share code, notes, and snippets.

@gm50x
Last active December 19, 2020 14:37
Show Gist options
  • Save gm50x/dd57c7b9b1e085dd40beacefaa820d39 to your computer and use it in GitHub Desktop.
Save gm50x/dd57c7b9b1e085dd40beacefaa820d39 to your computer and use it in GitHub Desktop.
Promisified a callback function
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