Skip to content

Commit

Permalink
[promise] implement untilAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed May 14, 2024
1 parent e8bb155 commit 9b54e80
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
1 change: 0 additions & 1 deletion indexeddbV2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,3 @@ export const testBlocked = async () => {
return idb.deleteDB(testDBName)
}).promise()
}

4 changes: 2 additions & 2 deletions pledge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const testPledgeCoroutine = async _tc => {
* @param {t.TestCase} _tc
*/
export const testPledgeVsPromisePerformanceTimeout = async _tc => {
const iterations = 25000
const iterations = 100
const waitTime = 0
await t.measureTimeAsync(`Awaiting ${iterations} callbacks (promise)`, async () => {
for (let i = 0; i < iterations; i++) {
Expand All @@ -48,7 +48,7 @@ export const testPledgeVsPromisePerformanceTimeout = async _tc => {
* @param {t.TestCase} _tc
*/
export const testPledgeVsPromisePerformanceResolved = async _tc => {
const iterations = 25000
const iterations = 100
t.measureTime(`Awaiting ${iterations} callbacks (only iterate)`, () => {
for (let i = 0; i < iterations; i++) { /* nop */ }
})
Expand Down
20 changes: 19 additions & 1 deletion promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const resolveWith = res => Promise.resolve(res)

/**
* @todo Next version, reorder parameters: check, [timeout, [intervalResolution]]
* @deprecated use untilAsync instead
*
* @param {number} timeout
* @param {function():boolean} check
Expand All @@ -80,11 +81,28 @@ export const until = (timeout, check, intervalResolution = 10) => create((resolv
const intervalHandle = setInterval(untilInterval, intervalResolution)
})

/**
* @param {()=>Promise<boolean>|boolean} check
* @param {number} timeout
* @param {number} intervalResolution
* @return {Promise<void>}
*/
export const untilAsync = async (check, timeout = 0, intervalResolution = 10) => {
const startTime = time.getUnixTime()
const noTimeout = timeout <= 0
// eslint-disable-next-line no-unmodified-loop-condition
while (noTimeout || time.getUnixTime() - startTime <= timeout) {
if (await check()) return
await wait(intervalResolution)
}
throw new Error('Timeout')
}

/**
* @param {number} timeout
* @return {Promise<undefined>}
*/
export const wait = timeout => create((resolve, reject) => setTimeout(resolve, timeout))
export const wait = timeout => create((resolve, _reject) => setTimeout(resolve, timeout))

/**
* Checks if an object is a promise using ducktyping.
Expand Down
3 changes: 3 additions & 0 deletions promise.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ export const testRepeatPromise = async _tc => {
await promise.resolve()
await measureP(promise.wait(10), 7, 1000)
await measureP(failsP(promise.until(15, () => false)), 15, 1000)
await measureP(failsP(promise.untilAsync(() => false, 15)), 15, 1000)
const startTime = time.getUnixTime()
await measureP(promise.until(0, () => (time.getUnixTime() - startTime) > 100), 100, 1000)
const startTime2 = time.getUnixTime()
await measureP(promise.untilAsync(() => (time.getUnixTime() - startTime2) > 100), 100, 1000)
await promise.all([promise.wait(5), promise.wait(10)])
}

Expand Down

0 comments on commit 9b54e80

Please sign in to comment.