【发布时间】:2016-12-11 05:58:15
【问题描述】:
我正在寻找一个 Promise 函数包装器,它可以在给定的 Promise 运行时限制/节流,以便在给定的时间只运行一定数量的 Promise。
在下面的情况下,delayPromise 不应同时运行,它们都应按先到先得的顺序一次运行一个。
import Promise from 'bluebird'
function _delayPromise (seconds, str) {
console.log(str)
return Promise.delay(seconds)
}
let delayPromise = limitConcurrency(_delayPromise, 1)
async function a() {
await delayPromise(100, "a:a")
await delayPromise(100, "a:b")
await delayPromise(100, "a:c")
}
async function b() {
await delayPromise(100, "b:a")
await delayPromise(100, "b:b")
await delayPromise(100, "b:c")
}
a().then(() => console.log('done'))
b().then(() => console.log('done'))
关于如何设置这样的队列有什么想法吗?
我有一个来自美妙Benjamin Gruenbaum 的“去抖”功能。我需要修改它以根据它自己的执行而不是延迟来限制承诺。
export function promiseDebounce (fn, delay, count) {
let working = 0
let queue = []
function work () {
if ((queue.length === 0) || (working === count)) return
working++
Promise.delay(delay).tap(function () { working-- }).then(work)
var next = queue.shift()
next[2](fn.apply(next[0], next[1]))
}
return function debounced () {
var args = arguments
return new Promise(function (resolve) {
queue.push([this, args, resolve])
if (working < count) work()
}.bind(this))
}
}
【问题讨论】:
-
async.js 和 queue.js 都支持可配置的并发。
-
用于数组或管理给定函数及其实例的状态?
-
when a given promise is running so that only a set number of that promise is running at a given time- 代码有 6 个 Promise,每个 Promise 将只运行一次 - 同时运行,但给定的 Promise 只运行一次 - 这个问题充其量是措辞不佳 -
这里要清楚一点。承诺不会“运行”。 Promise 是已经运行的异步操作结果的代理。
-
es6-promise-pool?promise-limit?cwait?不相关或未知?
标签: javascript node.js promise bluebird