递归异步延迟承诺重叠

时间:2018-05-24 15:03:01

标签: javascript recursion scope promise closures

用例如下:我想每隔X毫秒查询一次URL,直到得到肯定答复。

我正在尝试在JS(节点)中实现此功能,并且存在多个此类请求的问题。 如果我只启动其中一个“连锁电话”,一切正常。

但是如果我尝试并行运行一些“最后一个获胜”(并且第一个永远不会解决)。我似乎在使用对同一个对象或类似物的引用,但我无法弄明白。

注意“masterCounter”和“id”变量如何被覆盖,尽管它们是按值传递的参数。

这是工作代码(其中“SECOND”胜过“FIRST”):

(如果您安装了请求包https://www.npmjs.com/package/request,则可以使用节点运行)

var req = require('request');

const ASK_EVERY_MS = 500;
const AT_MOST_MS = 5000;

var childRecursive = (t, resolve, reject, t0, id, masterCounter) => {    
    var duration = process.hrtime(t0);    
    duration = (duration[0] * Math.pow(10, 9) + duration[1]) / Math.pow(10, 6)
    if (duration + t > AT_MOST_MS) {
        reject('timeout');
    } else {
        var options = {
            url: 'http://www.google.com',
            json: false,
            headers: {
                timeout: 1000
            }
        };
        callback = function (error, response, body) {
            if (!error) {
                console.log('body', body.substring(0, 100));
                if (masterCounter % 5 == 0) {
                    return resolve("success!!!");
                } else {
                    return childRecursive(ASK_EVERY_MS, resolve, reject, t0, id, masterCounter + 1, req);
                }
            } else {
                console.log('problem with submission GET request: ' + error);
                return reject(error);
            }
        }
        console.log(id + ' masterCounter is ' + masterCounter);
        setTimeout(() => {
            console.log(id + ' requesting...');
            req.get(options, callback);
        }, t);
    }
};

var t0 = process.hrtime();
var first = new Promise((resolve, reject) => childRecursive(0, resolve, reject, t0, 'FIRST', 101));
first.then(() => {
    console.log('FIRST done');
});
var scnd = new Promise((resolve, reject) => childRecursive(0, resolve, reject, t0, 'SECOND', 201));
scnd.then(() => {
   console.log('SECOND done');
});

和输出:

FIRST masterCounter is 101
SECOND masterCounter is 201
FIRST requesting...
SECOND requesting...
body <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"><head><meta content
SECOND masterCounter is 202
body <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"><head><meta content
SECOND masterCounter is 202
SECOND requesting...
SECOND requesting...
body <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"><head><meta content
SECOND masterCounter is 203
body <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"><head><meta content
SECOND masterCounter is 203
SECOND requesting...
SECOND requesting...
body <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"><head><meta content
SECOND masterCounter is 204
body <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"><head><meta content
SECOND masterCounter is 204
SECOND requesting...
SECOND requesting...
body <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"><head><meta content
SECOND masterCounter is 205
body <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"><head><meta content
SECOND masterCounter is 205
SECOND requesting...
SECOND requesting...
body <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"><head><meta content
SECOND done
body <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"><head><meta content

1 个答案:

答案 0 :(得分:1)

您的问题是implicitly global callback变量。它必须是

var callback = function (error, response, body) { /*
^^^ */

function callback(error, response, body) {

始终use strict mode以避免此类错误!