gpt4 book ai didi

javascript - 限制正在运行的 promise 的并发

转载 作者:数据小太阳 更新时间:2023-10-29 05:29:00 26 4
gpt4 key购买 nike

我正在寻找一个 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 的“去抖动”功能。我需要修改它以根据它自己的执行而不是延迟来限制 promise 。

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))
}
}

最佳答案

我不认为有任何库可以做到这一点,但实际上自己实现起来非常简单:

function queue(fn) { // limitConcurrency(fn, 1)
var q = Promise.resolve();
return function(x) {
var p = q.then(function() {
return fn(x);
});
q = p.reflect();
return p;
};
}

对于多个并发请求,它变得有点棘手,但也可以做到。

function limitConcurrency(fn, n) {
if (n == 1) return queue(fn); // optimisation
var q = null;
var active = [];
function next(x) {
return function() {
var p = fn(x)
active.push(p.reflect().then(function() {
active.splice(active.indexOf(p), 1);
})
return [Promise.race(active), p];
}
}
function fst(t) {
return t[0];
}
function snd(t) {
return t[1];
}
return function(x) {
var put = next(x)
if (active.length < n) {
var r = put()
q = fst(t);
return snd(t);
} else {
var r = q.then(put);
q = r.then(fst);
return r.then(snd)
}
};
}

顺便说一句,您可能想看看 actors modelCSP .他们可以简化处理此类事情的过程,那里也有一些 JS 库。

示例

import Promise from 'bluebird'

function sequential(fn) {
var q = Promise.resolve();
return (...args) => {
const p = q.then(() => fn(...args))
q = p.reflect()
return p
}
}

async function _delayPromise (seconds, str) {
console.log(`${str} started`)
await Promise.delay(seconds)
console.log(`${str} ended`)
return str
}

let delayPromise = sequential(_delayPromise)

async function a() {
await delayPromise(100, "a:a")
await delayPromise(200, "a:b")
await delayPromise(300, "a:c")
}

async function b() {
await delayPromise(400, "b:a")
await delayPromise(500, "b:b")
await delayPromise(600, "b:c")
}

a().then(() => console.log('done'))
b().then(() => console.log('done'))

// --> with sequential()

// $ babel-node test/t.js
// a:a started
// a:a ended
// b:a started
// b:a ended
// a:b started
// a:b ended
// b:b started
// b:b ended
// a:c started
// a:c ended
// b:c started
// done
// b:c ended
// done

// --> without calling sequential()

// $ babel-node test/t.js
// a:a started
// b:a started
// a:a ended
// a:b started
// a:b ended
// a:c started
// b:a ended
// b:b started
// a:c ended
// done
// b:b ended
// b:c started
// b:c ended
// done

关于javascript - 限制正在运行的 promise 的并发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38778723/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com