承诺的进展 [英] Progress of promises

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

问题描述

理论:我一开始就有大约100个诺言,然后使用 Promise.all()来解决它们.

这100个承诺中的每一个依次进行一些异步REST调用,这些响应的响应可能主要不同(例如,由于网络连接).

解决所有100个承诺的过程大约需要20秒.在此期间,应向用户提供进度的实时反馈,以保持其参与度.

为了实现这些异步操作的进度,我正在考虑在客户端上使用一个progressCounter,其价值将在每个promise解析后立即更新.

问题是,如果progressCounter = 1并且所有这些操作都是异步的,那么我担心会遇到竞争状况,例如,可能会发现两个不同的Promise检索到的progressCounter的当前值相同,即1,因此他们可以尝试将progressCounter递增到相同的值,即2.因此,由于竞争条件,最终值将不会是3.

实验:我试图重现这种理论,但不能使用以下内容:

  var progress = {};progress.counter = 1;var promise1 = new Promise(function(resolve,reject)){解决();});var promise2 = new Promise(function(resolve,reject)){解决();});promise1.then(function(){progress.counter + = 1;});promise2.then(function(){progress.counter + = 1;});setTimeout(function(){警报(progress.counter);//progress.counter:3},1000);` 

问题:问题是这样的比赛条件会受到以上理论描述的打击吗?如果不是,该理论有何缺陷?如果是,那么跟踪承诺解决进度的好方法是什么?

解决方案

问题:问题是这样的比赛条件会受到上述理论的影响吗?如果没有,那么该理论有何缺陷?

答案为,在Javascript中不会发生这种竞争情况,因为Javascript是单线程的.(请参见MDN上的并发模型和事件循环)

这意味着,当一个回调处理程序正在处理数据(假设设置计数器为同步操作,即+ =是)时,没有什么可以强迫它屈服"其执行,下一个处理程序只能在上一个处理程序完成后运行.

Theory: I have around 100 promises which I make in start and then later resolve them using Promise.all().

Each of those 100 promises in turn make some async REST calls whose response may vary mainly (for example due to network connectivity).

Process of resolving all 100 promises take around 20 sec. During that time user should be given a live feedback of progress to keep him engaged.

In order to implement progress of these async operations I am thinking of using a progressCounter on client end whose value will be updated by each promise as soon as its resolved.

Thing is that if progressCounter = 1 and as all those operations are async, I fear to hit a race condition where, for example, current value of progressCounter retrieved by two distinct promises might be found as same i.e. 1 so they may try to increment progressCounter to same value i.e. 2. So final value won't be 3 because of the race condition.

Experiment: I tried to reproduce this theory but couldn't using following:

var progress = {};
progress.counter = 1;

var promise1 = new Promise(function(resolve, reject) {
   resolve();
});

var promise2 = new Promise(function(resolve, reject) {
   resolve();
});

promise1.then(function() {
    progress.counter += 1;
});

promise2.then(function() {
    progress.counter += 1;
});

setTimeout(function() {
    alert(progress.counter); // progress.counter: 3
}, 1000);` 

Question: Question is can such a race condition be hit described in theory above? If not, how is the theory flawed? If yes, what is a good way to track progress of resolution of the promises?

解决方案

Question: Question is can such a race condition be hit described in theory above? If not, how is the theory flawed?

The answer is no, such a race condition can not occur in Javascript, because Javascript is single-threaded. (see: Concurrency Model and Event Loop on MDN)

This means that while one callback handler is working with the data (assuming that setting the counter is a synchronous operation, which += is), nothing can force it to "yield" its execution, the next handler can only run when the previous one has finished.

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

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