Fix withTimeout helper function
This commit is contained in:
parent
c6c7d293ca
commit
6e1dab28b6
6 changed files with 48 additions and 6 deletions
11
lib/util.js
generated
11
lib/util.js
generated
|
|
@ -754,13 +754,20 @@ exports.tryGetFolderBytes = tryGetFolderBytes;
|
|||
* @returns The result of the promise, or undefined if the promise times out.
|
||||
*/
|
||||
async function withTimeout(timeoutMs, promise, onTimeout) {
|
||||
let finished = false;
|
||||
const mainTask = async () => {
|
||||
const result = await promise;
|
||||
finished = true;
|
||||
return result;
|
||||
};
|
||||
const timeout = new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
onTimeout();
|
||||
if (!finished)
|
||||
onTimeout();
|
||||
resolve(undefined);
|
||||
}, timeoutMs);
|
||||
});
|
||||
return await Promise.race([promise, timeout]);
|
||||
return await Promise.race([mainTask(), timeout]);
|
||||
}
|
||||
exports.withTimeout = withTimeout;
|
||||
//# sourceMappingURL=util.js.map
|
||||
File diff suppressed because one or more lines are too long
14
lib/util.test.js
generated
14
lib/util.test.js
generated
|
|
@ -424,4 +424,18 @@ const shortTime = 10;
|
|||
t.deepEqual(shortTaskTimedOut, false);
|
||||
t.deepEqual(result, 99);
|
||||
});
|
||||
(0, ava_1.default)("withTimeout doesn't call callback if promise resolves", async (t) => {
|
||||
let shortTaskTimedOut = false;
|
||||
const shortTask = new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve(99);
|
||||
}, shortTime);
|
||||
});
|
||||
const result = await util.withTimeout(100, shortTask, () => {
|
||||
shortTaskTimedOut = true;
|
||||
});
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
t.deepEqual(shortTaskTimedOut, false);
|
||||
t.deepEqual(result, 99);
|
||||
});
|
||||
//# sourceMappingURL=util.test.js.map
|
||||
File diff suppressed because one or more lines are too long
|
|
@ -632,3 +632,18 @@ test("withTimeout on short task", async (t) => {
|
|||
t.deepEqual(shortTaskTimedOut, false);
|
||||
t.deepEqual(result, 99);
|
||||
});
|
||||
|
||||
test("withTimeout doesn't call callback if promise resolves", async (t) => {
|
||||
let shortTaskTimedOut = false;
|
||||
const shortTask = new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve(99);
|
||||
}, shortTime);
|
||||
});
|
||||
const result = await util.withTimeout(100, shortTask, () => {
|
||||
shortTaskTimedOut = true;
|
||||
});
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
t.deepEqual(shortTaskTimedOut, false);
|
||||
t.deepEqual(result, 99);
|
||||
});
|
||||
|
|
|
|||
10
src/util.ts
10
src/util.ts
|
|
@ -909,12 +909,18 @@ export async function withTimeout<T>(
|
|||
promise: Promise<T>,
|
||||
onTimeout: () => void
|
||||
): Promise<T | undefined> {
|
||||
let finished = false;
|
||||
const mainTask = async () => {
|
||||
const result = await promise;
|
||||
finished = true;
|
||||
return result;
|
||||
};
|
||||
const timeout: Promise<undefined> = new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
onTimeout();
|
||||
if (!finished) onTimeout();
|
||||
resolve(undefined);
|
||||
}, timeoutMs);
|
||||
});
|
||||
|
||||
return await Promise.race([promise, timeout]);
|
||||
return await Promise.race([mainTask(), timeout]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue