Skip to content

Instantly share code, notes, and snippets.

@jessecogollo
Created October 23, 2017 06:44
Show Gist options
  • Save jessecogollo/c5ea3773e4426c4374a175d22376af6e to your computer and use it in GitHub Desktop.
Save jessecogollo/c5ea3773e4426c4374a175d22376af6e to your computer and use it in GitHub Desktop.
promisify
'use strict';
const {
cbFunction,
cbFunctionPromisify
} = require('./modules/util');
const dataError = {error: true, message: 'I am an unicorn !!!'};
const dataResult = {error: false, message: 'I am an unicorn !!!'};
cbFunction(dataError, (err, result) => {
if (err) {
console.log('cb0 error: ', err);
}
console.log('cb0 result', result);
});
cbFunction(dataResult, (err, result) => {
if (err) {
console.log('cb1 error: ', err);
}
console.log('cb1 result', result);
});
cbFunctionPromisify(dataError)
.then((result) => {
console.log('p0 result', result);
})
.catch((err) => {
console.log('p0 error: ', err);
});
cbFunctionPromisify(dataResult)
.then((result) => {
console.log('p1 result', result);
})
.catch((err) => {
console.log('p1 error: ', err);
});
'use strict';
const {
promisify
} = require('util');
const cbFunction = (data, cb) => {
if (data.error === true) {
return cb('I am a error');
}
return cb(null, data.message);
};
const cbFunctionPromisify = promisify(cbFunction);
module.exports = {
cbFunction,
cbFunctionPromisify
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment