mirror of https://github.com/vector-im/riot-web
Rewrite limitConcurrency to fix error catching
Make sure that we only catch errors that are a result of calling fn()pull/21833/head
parent
da1a5616eb
commit
71c1198d12
|
@ -48,32 +48,32 @@ 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) => {
|
|
||||||
const item = () => {
|
|
||||||
ongoingRequestCount++;
|
|
||||||
resolve();
|
|
||||||
};
|
|
||||||
if (ongoingRequestCount >= LIMIT) {
|
if (ongoingRequestCount >= LIMIT) {
|
||||||
// Enqueue this request for later execution
|
// Enqueue this request for later execution
|
||||||
backlogQueue.push(item);
|
await new Promise((resolve, reject) => {
|
||||||
} else {
|
backlogQueue.push(resolve);
|
||||||
item();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then(fn)
|
|
||||||
.catch((err) => {
|
|
||||||
ongoingRequestCount--;
|
|
||||||
checkBacklog();
|
|
||||||
throw err;
|
|
||||||
})
|
|
||||||
.then((result) => {
|
|
||||||
ongoingRequestCount--;
|
|
||||||
checkBacklog();
|
|
||||||
return result;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let result;
|
||||||
|
let error;
|
||||||
|
|
||||||
|
ongoingRequestCount++;
|
||||||
|
try {
|
||||||
|
result = await fn();
|
||||||
|
} catch (err) {
|
||||||
|
error = err;
|
||||||
|
} finally {
|
||||||
|
ongoingRequestCount--;
|
||||||
|
checkBacklog();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error) throw error;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Global store for tracking group summary, members, invited members and rooms.
|
* Global store for tracking group summary, members, invited members and rooms.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue