forked from sushi-labs/sushiswap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
49 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,33 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { describe, it } from 'vitest' | ||
import { promiseTimeout } from './timeout.js' | ||
|
||
const sleep = async(ms: number, msg = "") => { | ||
let _timeoutReference; | ||
return new Promise( | ||
resolve => _timeoutReference = setTimeout(() => resolve(msg), ms), | ||
).finally( | ||
() => clearTimeout(_timeoutReference) | ||
); | ||
}; | ||
const sleep = async (ms: number, msg = '') => { | ||
let _timeoutReference | ||
return new Promise((resolve) => { | ||
_timeoutReference = setTimeout(() => resolve(msg), ms) | ||
return _timeoutReference | ||
}).finally(() => clearTimeout(_timeoutReference)) | ||
} | ||
|
||
describe('Promise Timeout', async () => { | ||
it('should timeout', async () => { | ||
const mainPromise = sleep(1000); | ||
const mainPromise = sleep(1000) | ||
await promiseTimeout(mainPromise, 500) | ||
.then(() => { | ||
throw "expected to reject, but resolved" | ||
throw 'expected to reject, but resolved' | ||
}) | ||
.catch(() => { | ||
/**/ | ||
}) | ||
|
||
}) | ||
it('should NOT timeout', async () => { | ||
const mainPromise = sleep(1000); | ||
const mainPromise = sleep(1000) | ||
await promiseTimeout(mainPromise, 1500) | ||
.then(() => { | ||
/**/ | ||
}) | ||
.catch(() => { | ||
throw "expected to resolve, but rejected" | ||
throw 'expected to resolve, but rejected' | ||
}) | ||
|
||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
export async function promiseTimeout (promise: Promise<any>, time: number, exception?: any) { | ||
let timer: any; | ||
export async function promiseTimeout( | ||
promise: Promise<any>, | ||
time: number, | ||
exception?: any, | ||
) { | ||
let timer: any | ||
return Promise.race([ | ||
promise, | ||
new Promise( | ||
(_res, _rej) => timer = setTimeout(_rej, time, exception) | ||
) | ||
]).finally( | ||
() => clearTimeout(timer) | ||
); | ||
}; | ||
promise, | ||
new Promise((_res, _rej) => { | ||
timer = setTimeout(_rej, time, exception) | ||
return timer | ||
}), | ||
]).finally(() => clearTimeout(timer)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters