mirror of
https://github.com/element-hq/element-web
synced 2024-11-23 17:56:01 +03:00
Rewrite limitConcurrency to fix error catching
Make sure that we only catch errors that are a result of calling fn()
This commit is contained in:
parent
da1a5616eb
commit
71c1198d12
1 changed files with 22 additions and 22 deletions
|
@ -48,30 +48,30 @@ function checkBacklog() {
|
||||||
|
|
||||||
// Limit the maximum number of ongoing promises returned by fn to LIMIT and
|
// Limit the maximum number of ongoing promises returned by fn to LIMIT and
|
||||||
// use a FIFO queue to handle the backlog.
|
// use a FIFO queue to handle the backlog.
|
||||||
function limitConcurrency(fn) {
|
async function limitConcurrency(fn) {
|
||||||
return new Promise((resolve, reject) => {
|
if (ongoingRequestCount >= LIMIT) {
|
||||||
const item = () => {
|
// Enqueue this request for later execution
|
||||||
ongoingRequestCount++;
|
await new Promise((resolve, reject) => {
|
||||||
resolve();
|
backlogQueue.push(resolve);
|
||||||
};
|
});
|
||||||
if (ongoingRequestCount >= LIMIT) {
|
}
|
||||||
// Enqueue this request for later execution
|
|
||||||
backlogQueue.push(item);
|
let result;
|
||||||
} else {
|
let error;
|
||||||
item();
|
|
||||||
}
|
ongoingRequestCount++;
|
||||||
})
|
try {
|
||||||
.then(fn)
|
result = await fn();
|
||||||
.catch((err) => {
|
} catch (err) {
|
||||||
|
error = err;
|
||||||
|
} finally {
|
||||||
ongoingRequestCount--;
|
ongoingRequestCount--;
|
||||||
checkBacklog();
|
checkBacklog();
|
||||||
throw err;
|
}
|
||||||
})
|
|
||||||
.then((result) => {
|
if (error) throw error;
|
||||||
ongoingRequestCount--;
|
|
||||||
checkBacklog();
|
return result;
|
||||||
return result;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue