2 ms·
One of my favourite parts of Bluebird is the 'Promisify' function. It allows you to take existing Node modules and use them as promises. Here's an example of in
by untog 12y ago
One of my favourite parts of Bluebird is the 'Promisify' function. It allows you to take existing Node modules and use them as promises. Here's an example of interating over an array of files, only writing ones that don't already exist:
http://pastie.org/9559209 http://pastie.org/9559209
It's also one of the reasons that I've ended up liking CoffeeScript more - Promises have a lot of function() and return statements, CS makes that kind of code a lot cleaner. For instance, that JS could be this short in CS:
Promise.filter files, (file) ->
fs.existsAsync(file).then (doesExist) -> !doesExist
.each (file) ->
fs.writeFileAsync(file.filename,file.content)
.then ->
Promise.map files, (file) -> fs.readFileAsync(file,'UTF-8')
.then (fileContents) ->
console.log(fileContents);
###
Will return:
[
"File 1 Content",
"File 4 Content",
"File 3 Content"
]
###
.catch (err) ->
# This will catch any and all errors that happened above.