具有延迟的顺序承诺循环 [英] Sequential promise loop with delay

查看:71
本文介绍了具有延迟的顺序承诺循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按顺序加载一系列请求,每个请求都被延迟隔开。

I'm attempting to load an array of "requests" sequentially each one separated by a delay.

我正在使用承诺,但由于某些原因,我是问题是并行执行而不是按顺序执行。

I'm using promises but for some reasons I'm having issues with the requests being executed in parallel instead of in sequence.

我写了一些测试代码,如下所示。有用!它发出请求,处理它,timeOut 3秒,然后转到第二个请求。

I wrote some test code as per below. It works! It makes the request, processes it, timesOut for 3 second and goes to the second request.

var batches = [of_objects];
var sequence = Promise.resolve();
var self = this;

sequence
    //Create request for first batch
    .then( function(){
        return self._createRequestPromise(batches[0]);
    })
    //callback so we can update, returns nothing
    .then(callback)
    //add 3 sec timeout before next request
    .then(function(){
        return new Promise(function (resolve, reject){
            setTimeout(resolve, 3000);
        });
    })
    //Create request for second batch
    .then( function(){
        return self._createRequestPromise(batches[1]);
    })
    //callback so we can update, returns nothing
    .then(callback)
    //add 3 sec timeout before next request
    .then(function(){
        return new Promise(function (resolve, reject){
            setTimeout(resolve, 3000);
        });
    });

return sequence;

但是,一旦我尝试把它放到任何类型的循环,我的请求是同时解雇所有。并且超时调用发生在。

BUT, as soon as I try to put it any sort of loop, my requests are firing all at the same time. And the timeout calls happen after.

我不确定我做错了什么,或者误解了。

I'm not sure what I'm doing wrong, or misunderstanding.

//object I need to use to construct requests
var batches = [of_objects];
var sequence = Promise.resolve();
var self = this;

batches.forEach(function(batch){
    sequence
    //Function returns promise
    .then( function(){
        return self._createRequestPromise(batch); //this runs first 5x
    })
    //callback so we can update, returns nothing
    .then(callback)
    //add 3 sec timeout before next request
    .then(function(){
        return new Promise(function (resolve, reject){
            setTimeout(resolve, 3000); //this runs after 5x
        });
    });
});
return sequence;

推荐答案

问题是你不是你pdating sequence 包含任何连续的操作,因此所有这些操作都链接到原始解析的promise。没有什么可以延迟任何一个,所以他们立即执行。

The issue is that you're not updating sequence to include any of the successive operations, so all of them are chaining off of that original resolved promise. There's nothing there to delay any of them, so they execute right away.

你应该能够通过更新序列来解决这个问题变量,因此你链接到链的末尾:

You should be able to remedy this simply by updating the sequence variable on every loop, so you're chaining off the end of the chain:

//object I need to use to construct requests
var batches = [of_objects];
var sequence = Promise.resolve();
var self = this;

batches.forEach(function (batch){
    sequence = sequence            // <-- here
        //Function returns promise
        .then( function(){
            return self._createRequestPromise(batch); //this runs first 5x
        })
        //callback so we can update, returns nothing
        .then(callback)
        //add 3 sec timeout before next request
        .then(function(){
            return new Promise(function (resolve, reject){
                setTimeout(resolve, 3000); //this runs after 5x
            });
        });
});

return sequence;

就个人而言,我尽量避免覆盖变量,因此在没有变量重新分配的情况下执行此操作的另一种方法是使用 .reduce()

Personally, I try to avoid overwriting variables, so an alternate way to do this without variable reassignment is to use .reduce():

//object I need to use to construct requests
var batches = [of_objects];
var self = this;

return batches.reduce(function (last, batch){
    return last
        //Function returns promise
        .then( function(){
            return self._createRequestPromise(batch); //this runs first 5x
        })
        //callback so we can update, returns nothing
        .then(callback)
        //add 3 sec timeout before next request
        .then(function(){
            return new Promise(function (resolve, reject){
                setTimeout(resolve, 3000); //this runs after 5x
            });
        });
}, Promise.resolve());

这篇关于具有延迟的顺序承诺循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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