I'm working my way through the Node Beginner Tutorial while tailoring things to work with my development style. For example I'm selecting to use coffeescript, and requirejs. The later involves a great deal of callback-soup on top of the significant regular amount. Usually these are situations I like to deal with using promises. After some minimal research promise-io seems like a reasonable solution. It works but suddenly I'm running into a problem combining promises using all (which seems analagous to jQuery.when - where this is a technique I use frequently).
The following is my server.coffee file. As is it works perfectly, replacing the nested deferreds with the all
call serves one request then hangs the server. Does anyone have any idea what the problem is?
exports.start = ->
requirejs ['http', 'url', './route'], (http, url, route)->
console.log "Starting server"
server = http.createServer (request, response) ->
pathdata = url.parse request.url
###
#this breaks
promise.all(readingPost(request), route.promise).then (x) ->
[postData, route] = x
route pathdata, response, postData
###
#this works
readingPost(request).then (postData) -> route.promise.then (route) ->
route pathdata, response, postData
server.listen 8888
readingPost = (request) ->
request.setEncoding 'utf-8'
data = ''
request.addListener 'data', (dataChunk) ->
data += dataChunk
d = promise.Deferred()
request.addListener 'end', -> d.resolve data
d
At first glance, your first example will overwrite the route
that you loaded with requirejs
, so the second time it runs, you will have totally different value.
Try changing
[postData, route] = x
route pathdata, response, postData
to
[postData, routeCb] = x
routeCb pathdata, response, postData