异步并等待 [英] Async and await

查看:256
本文介绍了异步并等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为zipCode绘制标记.但是我只能看到一些标记. 我以为是因为异步和等待,但是我不知道在哪里添加它们. 有人请帮助我.

I want to draw markers for zipCode. But I can see only a few markers. I thought it was because of async and await, but I don't know where to add them. Somebody please help me.

var zipCode=[...]; //zipCode is array of zip codes.
function func1() {
    zipCode.forEach((item, index) => {
        drawZipCodeMarker(item.zip);
    });
}


function drawZipCodeMarker(zip){
       geocoder.geocode({'address':zip}, (results, status) => {
            console.log(zip);
            console.log(results);
            if (results != null) {
                var temp = new google.maps.Marker({position : results[0].geometry.location, map:map, title:zip});
            }
        });
    }

推荐答案

您正在使用Maps JavaScript API的地址解析服务. Google Maps JavaScript API中的服务具有每个会话限制,如文档中所述.

You are using Geocoding service of Maps JavaScript API. The services in Google Maps JavaScript API have a per session limit described in the documentation as.

注意:无论有多少用户共享同一个项目,每个用户会话都会应用附加的速率限制.首次加载API时,系统会为您分配初始的请求配额.使用此配额后,API会每秒对其他请求强制执行速率限制.如果在特定时间段内提出了太多请求,则API将返回OVER_QUERY_LIMIT响应代码.

Note: The additional rate limit is applied per user session, regardless of how many users share the same project. When you first load the API, you are allocated an initial quota of requests. Once you use this quota, the API enforces rate limits on additional requests on a per-second basis. If too many requests are made within a certain time period, the API returns an OVER_QUERY_LIMIT response code.

每个会话的速率限制禁止将客户端服务用于批处理请求,例如批处理地理编码.对于批处理请求,请使用Geocoding API Web服务.

The per-session rate limit prevents the use of client-side services for batch requests, such as batch geocoding. For batch requests, use the Geocoding API web service.

来源: https://developers.google.com/maps/documentation/javascript /geocoding

据我所知,最初您有10个请求.一旦存储桶为空,请求将被拒绝.桶以每秒1个请求的速率重新填充.因此,您必须限制地理编码请求,才能将其限制在每个会话允许的范围内.

As far as I know, initially you have a bucket of 10 requests. Once the bucket is empty request is denied. The bucket is refilled at the rate 1 request per second. So, you have to throttle your geocoding requests in order to stay within allowed per session limits.

您应该检查响应的状态.如果状态为OVER_QUERY_LIMIT,则说明您已耗尽存储桶,需要重试该请求.您可以使用指数退避方法重试逻辑( https://en.wikipedia.org/wiki/Exponential_backoff ).

You should check the status of the response. If status is OVER_QUERY_LIMIT, so you exhausted your bucket and need retry the request. You can use Exponential Backoff approach for retrying logic (https://en.wikipedia.org/wiki/Exponential_backoff).

var zipCode=[...]; //zipCode is array of zip codes.
var delayFactor = 0;

function func1() {
    zipCode.forEach((item, index) => {
        drawZipCodeMarker(item.zip);
    });
}

function drawZipCodeMarker(zip) {
   geocoder.geocode({'address':zip}, (results, status) => {
       if (status === google.maps.GeocoderStatus.OK) {
           console.log(zip);
           console.log(results);
           if (results != null) {
               var temp = new google.maps.Marker({position : results[0].geometry.location, map:map, title:zip});
           }
       } else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
           delayFactor++;
           setTimeout(function () {
               drawZipCodeMarker(zip)
           }, delayFactor * 1100);
       } else {
           console.log("Error: " + status);
       }
   });
}

我希望这会有所帮助!

这篇关于异步并等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆