Node 5 is awesome! <3 By default, it comes with lots of ES6 features.
Among them are, for example, arrow functions. And there's the spread operator which, in combination with the arguments object, makes writing a promisify()
function beautifully simple:
function promisify (foo) {
return function () {
return new Promise ((resolve, reject) => {
foo(...arguments, (error, result) => {
if (error) reject(error)
else resolve(result)
})
})
}
}
! The
function
in line 2 can't be replaced with an arrow function. That is because an arrow function does not bind its own arguments object.
And the best part of this, with ES7, soon you'll be able to await
your Promises in async
functions! <3
! With Babel, you can already use next generation Node today.