Created
October 23, 2017 06:44
-
-
Save jessecogollo/c5ea3773e4426c4374a175d22376af6e to your computer and use it in GitHub Desktop.
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
'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); | |
}); |
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
'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