首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绕过异步请求

绕过异步请求
EN

Stack Overflow用户
提问于 2013-08-19 11:00:44
回答 1查看 47关注 0票数 0

所以我有一个这样的函数-

代码语言:javascript
复制
    IPGeocoding = (data) ->
    coords = []
    _.each(data, (datum) ->
        $.ajax(
          url: "http://freegeoip.net/json/#{datum}"
          type: 'GET'
          async: false
          success: (result) ->
            lat = result.latitude
            lon = result.longitude
            pair = [lat, lon]
            coords.push(pair)
            console.log coords
        )

    return coords


    )

我希望仅当所有请求都已返回时才返回coords。我该怎么做?

EN

回答 1

Stack Overflow用户

发布于 2013-08-19 11:05:52

下划线与_.after方法捆绑在一起。_.after有两个参数。第二个是您想要执行的函数,第一个是您希望它在执行之前被调用的次数。您可以通过以下方式使用它来完成您想要做的事情:

代码语言:javascript
复制
IPGeocoding = (data, callback) ->
    coords = []
    finish = _.after(data.length, callback)
    _.each(data, (datum) ->
        $.ajax(
            url: "http://freegeoip.net/json/#{datum}"
            type: 'GET'
            async: false
            success: (result) ->
                lat = result.latitude
                lon = result.longitude
                pair = [lat, lon]
                coords.push(pair)
                console.log coords

                finish(coords)
         )
    )
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18305825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档