【问题标题】:node.js: how to use fibrous to return data from the request librarynode.js:如何使用 fibrous 从请求库中返回数据
【发布时间】:2015-01-13 06:04:23
【问题描述】:

我正在尝试将 fibrous 库与 request 一起使用以同步返回 HTTP 请求的正文,但我遇到了流程问题。

我写了一个非常简单的辅助函数:

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"))
)

如果不是,那么构建我的代码以使我获得对请求的同步调用的正确方法是什么?

【问题讨论】:

    标签: javascript node.js asynchronous coffeescript


    【解决方案1】:

    好的,我使用以下代码完成了这项工作:

    syncGETRequest = fibrous((url, options)->
        data = request.sync.get(url, options)
        console.log(data)
    )
    
    runTest = ()->
        syncGETRequest.sync("http://www.google.com", {})
    
    fibrous.run(runTest)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 2014-03-27
      • 2016-12-02
      • 2012-12-13
      • 1970-01-01
      相关资源
      最近更新 更多