失败时如何重新运行javascript承诺? [英] How to re-run a javascript promise when failed?

查看:52
本文介绍了失败时如何重新运行javascript承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,我尝试使用 Google Maps Geocoding API 将 businesses 数组中的 10-15 个地址列表转换为纬度/经度坐标.

I have the following code where I am trying to convert a list of 10-15 addresses in the businesses array into lat/lng coordinates using Google Maps Geocoding API.

var geocoder = new google.maps.Geocoder();

var proms = businesses.map(function(address) {
    return prom = new Promise(function(resolve) {
       geocoder.geocode({
            address: address
        }, function(results, status) {

            if (status === google.maps.GeocoderStatus.OK) {
                resolve({
                    results: results,
                    business: address
                });

            } else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
                setTimeout(function() {
                    //HOW DO I RE-RUN THIS "new Promise"?
                }, 1000);

            } else {
                console.log(status + ': ' + results);
            }

        });
    });
});
Promise.all(proms);

问题是 Google 的 API 有时会返回 OVER_QUERY_LIMIT 错误,所以我想知道是否有办法重新运行几秒钟后失败的承诺.在上面的例子中,else if 语句捕获了错误,那么我如何让它在超时后重新运行?

The problem is that Google's API will sometimes return an OVER_QUERY_LIMIT error so I was wondering if there's a way to re-run the promise that failed after a few seconds. In the example above the else if statement catches the error, so how would I make it rerun after a timeout?

请注意,我刚刚开始使用 Promise 并且对它们的工作原理还没有很好的理解,所以我的方法可能完全错误.此外,所有这些都是在假设 Google 的 API 不允许我批量解析地址的情况下完成的,如果情况并非如此 - 请告诉我.

Note that I just started using promises and don't have a good understanding yet how they work so my approach might be completely wrong. Also all of this is done under the assumption that Google's API won't let me resolve addresses in bulk, if that's not the case - please, let me know.

推荐答案

您可以将代码主体放入一个函数中,然后从计时器中再次调用它.注意:这在技术上不是重新运行承诺,而是重新运行一段代码,然后在完成后解决承诺:

You can put the main body of code into a function and call it again from the timer. Note: this is not technically rerunning a promise, it's rerunning a block of code that will then resolve the promise when done:

var proms = businesses.map(function(address) {
    return prom = new Promise(function(resolve, reject) {
        var retriesRemaining = 5;
        function run() {
            geocoder.geocode({
                    address: address
                }, function(results, status) {

                    if (status === google.maps.GeocoderStatus.OK) {
                        resolve({
                            results: results,
                            business: address
                        });

                    } else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
                        --retriesRemaining;
                        if (retriesRemaining <= 0) {
                            reject(status);
                        } else {
                            setTimeout(run, 1000);
                        }
                    } else {
                        console.log(status + ': ' + results);
                    }

                }
            });
        }
        run();
    });
});
Promise.all(proms);

仅供参考,我还添加了重试次数,这样代码就不会卡在错误循环中.

FYI, I also added a retry count so the code can never get stuck in an error loop.

您可能也对这篇文章感兴趣:如何避开谷歌地图地理编码限制?.

You may also be interested in this post: How to avoid Google map geocode limit?.

这篇关于失败时如何重新运行javascript承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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