2014-11-16 46 views
0

我試圖使用fibrous庫與request同步返回HTTP請求的主體,但我遇到了流的問題。node.js:如何使用fibrous從請求庫中返回數據

我寫了一個很簡單的輔助功能:

getPage = (url, callback)-> 
    request(url, (error, response, body) -> 
     if error 
      callback(error, null) 
     if not error and response.statusCode is 200 
      console.log ("got here") 
      callback(null, body) 
    ) 

如果我要變成一個同步調用,我只是這樣做:

syncRequest = fibrous((url)-> 
    console.log(sync.getPage("http://www.google.com")) 
) 

如果沒有,是什麼正確的方式來構建我的代碼,以便我得到一個同步調用請求?

回答

0

好,我得到這個用下面的代碼工作:

syncGETRequest = fibrous((url, options)-> 
    data = request.sync.get(url, options) 
    console.log(data) 
) 

runTest =()-> 
    syncGETRequest.sync("http://www.google.com", {}) 

fibrous.run(runTest) 
相關問題