简体   繁体   English

XHR进度事件微任务队列

[英]XHR progress event microtask queue

I want to use XHR with promises. 我想使用带有承诺的XHR。 I created a XHR Promise as suggested on HTML5 rocks (1) and Mozilla's website (2). 根据HTML5岩石(1)和Mozilla网站(2)的建议,我创建了一个XHR Promise。

It doesn't work as expected. 它没有按预期工作。 If I have the following sequence to load and process a file: 如果我按照以下顺序加载和处理文件:

sequence = xhrPromise('filename').
  .then(parseFile)
  .then(processFile)
  .then(allSet);

If I just run 1 sequence, it works as expected. 如果我只运行1个序列,它将按预期工作。

If I try to run N sequences with Promise.all() to download/process N files, it breaks. 如果我尝试使用Promise.all()运行N个序列以下载/处理N个文件,则会中断。

All the downloads start in "parallel", then once the first XHR request has completed, it stops the download of other files, run the full sequence for this file then resume the download of remaining files. 所有下载均以“并行”开始,然后,第一个XHR请求完成后,它将停止其他文件的下载,为该文件运行完整序列,然后继续下载其余文件。 Then, once another file is completed, it blocks the downloads, run the sequence for this files, then resumes the downloads and so on. 然后,一旦另一个文件完成,它将阻止下载,运行该文件的序列,然后继续下载等等。

It seems to be the expected behavior as XHR progress events are thrown the main tasks queue and that promises are using the micro task queue. 这似乎是预期的行为,因为XHR进度事件被抛出了主任务队列,并且承诺正在使用微任务队列。

Does it make sense? 是否有意义?

It seems that it works as expected if I use the "fetch" API instead of XHR but not being able to monitor the progress of a download is a deal breaker for me. 如果我使用“获取” API而不是XHR,但似乎可以按预期工作,但是对我来说,无法监视下载进度是一件很麻烦的事情。

Is there a good way to work around this issue? 有解决此问题的好方法吗?

(1) http://www.html5rocks.com/en/tutorials/es6/promises/ (1) http://www.html5rocks.com/zh-CN/tutorials/es6/promises/

(2) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise (2) https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise

it stops the download of other files 它停止下载其他文件

What do you mean by that? 你是什​​么意思? IO happens outside the main thread, downloads continue even if you execute javascript. IO发生在主线程之外,即使您执行javascript,下载仍会继续。 Although if you do some heavy processing on the results you might want to look into webworkers to not block the main UI thread. 尽管如果对结果进行一些繁重的处理,您可能希望调查Webworkers以免阻塞主UI线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM