In other words, the following asynchronous code:
var d = Domain.create()
d.on("error", function (error) {
console.error("Error with the twitterverse:", error)
})
d.enter()
getTweetsFor("domenic")
.reduce(findMostMentioned, [])
.map(function (mostMentioned) {
return mostMentioned.map(function (user) { return user.username })
})
.toArray(function (mostMentionedNames) {
console.log("Most mentioned names:", mostMentionedNames)
})
d.exit()
parallels the synchronous code:
try {
var mostMentioned = findMostMentioned(getTweetsFor("domenic"))
var mostMentionedNames = mostMentioned.map(function (user) { return user.username })
console.log("Most mentioned names:", mostMentionedNames)
} catch (error) {
console.error("Error with the twitterverse: ", error)
}
Note in particular how errors flowed from any step in the process to our catch handler, without explicit by-hand bubbling code.
You are exhibiting promises here exactly. Your
getTweetsFor
,reduce
, andmap
functions are promise-returning, andtoArray
is just a weakerthen
in disguise.