Node 8 functions

In node 8 a handy little function is the utils.promisify

Its very simple. It wraps an “old” callback styled function into a promise function:

const {promisify} = require('util');

const isPositive = (number, callback) => { 
    if (number > 0) return callback(null, number)
    else return callback('not positive')

}
const isPositivePromise = promisify(isPositive);

isPositivePromise(33) 
    .then(ans => isPositivePromise(ans*-1))
    .then(no => console.log('should not be here'))
    .catch(err => {
        console.log('err: ', err)
    })

This chain will end up in the catch, because of the second request too isPositivePromise.