如何限制角度 $q 承诺并发? [英] How can I limit angular $q promise concurrency?

查看:23
本文介绍了如何限制角度 $q 承诺并发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何做类似 $q.all 的事情,但限制同时执行的承诺数量?

我的问题就像如何限制Q promise并发?

<块引用>

我希望一次不超过 5 个进程

另一个问题的公认答案是一个为 promise 编写的库,包装为与 Q.但我对 Angular 的 $q 而不是 Q 的解决方案特别感兴趣.

背景:正在解决的问题:
我有一堆文件要分两步下载:a) 获取 URL b) 下载文件.
浏览器限制了可以同时检索的文件数量,因此当直接使用 $q.all 承诺触发所有下载时,只有
N 立即发生,例如6 在 Chrome 中,而其余的则延迟.(请参阅浏览器中的最大并行 http 连接数?)
问题是 URL 已经过期,所以当浏览器执行 N+1th 个文件下载时,URL 不再有效.
所以我想做一些像 throttled.all(6, promises) 而不是 $q.all(promise)

解决方案

如果 'all' Promises 的解决是无关紧要的(比如你正在更新页面上的一些元素,而你并不关心它是否是渐进的而不是一个'whoosh') 我创建了一个简单的 TaskQueue 服务.它不会等待所有 Promises 都被解决,而是会处理所有添加的 Promises/Functions/Tasks,并且最大.配置限制值的并发.

因为我只发现这个和其他一些 StackOverflows 对我的问题没有帮助.所以我现在想回馈社区.希望它可以帮助某人.

它使用现代 JS 的东西,比如 const 和 lambda 表达式,但如果你只想要旧"的东西,你可以简单地让它从像 babel 这样的预编译器编译下来.

https://gist.github.com/Blackskyliner/8b1bafed326044fa4f8b1ba2627d11p>11p>

它只是在添加了 Tasks 之后处理它的队列,Tasks 只是匿名函数,返回一个 promise 或 value.它将遵守服务上可配置的maxConcurrentTasks"变量.

如果任务返回一个返回一个承诺的承诺,依此类推,它将始终使用队列中的初始槽".因此,在解决(或拒绝)整个任务承诺链后,它将为其他添加的任务释放槽".

How do I do something like $q.all but limiting how many promises are executed concurrently?

My question is just like How can I limit Q promise concurrency?

I want no more than 5 process spawned at a time

The accepted answer for that other question is a library written for promise wrapped to work with Q. But I'm interested specifically in a solution for Angular's $q rather than for Q.

Background: The problem being solved:
I have a bunch of files to download in 2 steps: a) Get URL b) download file.
The browser limits how many files can be retrieved concurrently, so when the straightforward use of promises with $q.all fires off all the downloads, only
N happen right away, e.g. 6 in Chrome, while the rest are delayed. (see Max parallel http connections in a browser?)
Problem is that the URLs have expiry, so by the time the browser executes the N+1th file download, the URL is no longer valid.
So I want to do something like throttled.all(6, promises) rather than $q.all(promise)

解决方案

If the resolving of 'all' Promises is irrelevant (like you are updating some Elements on the page and you don't care if it's progressive and not in one 'whoosh') I have created a simple TaskQueue Service. It will not wait for all Promises to be resolved but rather it will process all added Promises/Functions/Tasks it gets and with a max. concurrency of a configured limit value.

As I only found this and some other StackOverflows not helping with my problem I had. So I now give something back to the community I guess. Hope it helps somebody.

It uses modern JS stuff like const and lambda expressions, but you may simply let it compile down from a precompiler like babel if you just want the 'old' stuff.

https://gist.github.com/Blackskyliner/8b1bafed326044fa4f8b1ba2627d1117

It just simply processes its queue after Tasks, which are just anonymous functions, returning a promise or value, are added. It will adhere to a configurable 'maxConcurrentTasks' variable on the service.

If the Task returns a promise which returns a promise and so on it will always use the initial 'slot' within the queue. So it will free the 'slot' for an other added Task after the whole Task Promise chain is resolved (or rejected).

这篇关于如何限制角度 $q 承诺并发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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