-
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.
feat: new function - onlyResolvesLast
- Loading branch information
Showing
5 changed files
with
79 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { onlyResolvesLast } from './onlyResolvesLast' | ||
|
||
describe('onlyResolvesLast', () => { | ||
it('should resolve the last call', async () => { | ||
const foo_ = async (n: number) => { | ||
return new Promise<number>((resolve) => { | ||
setTimeout(() => resolve(n), Math.random()) | ||
}) | ||
} | ||
const foo = onlyResolvesLast(foo_) | ||
let lastValue = 0 | ||
|
||
await Promise.race([ | ||
foo(1).then(v => lastValue = v), | ||
foo(2).then(v => lastValue = v), | ||
foo(3).then(v => lastValue = v), | ||
foo(4).then(v => lastValue = v), | ||
]) | ||
expect(lastValue).toBe(4) | ||
}) | ||
it('should reject the last call', async () => { | ||
const foo_ = async (n: number) => { | ||
return new Promise<number>((resolve, reject) => { | ||
setTimeout(() => reject(n), Math.random()) | ||
}) | ||
} | ||
const foo = onlyResolvesLast(foo_) | ||
let lastError = 0 | ||
|
||
await Promise.race([ | ||
foo(1).catch(e => lastError = e), | ||
foo(2).catch(e => lastError = e), | ||
foo(3).catch(e => lastError = e), | ||
]) | ||
expect(lastError).toBe(3) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Wraps an async function, and ensures that only last call will ever resolve/reject | ||
* @param {T} fn - T - the function to wrap | ||
* @returns A function that will only resolve the last call. | ||
* @example | ||
* ``` | ||
const foo_ = (id: number) => fetch('/api/post/' + id) | ||
const foo = onlyResolvesLast(foo) | ||
foo(1).then(() => console.log('resolved')) // promise will never resolve | ||
foo(2).then(() => console.log('resolved')) // promise will never resolve | ||
foo(3).then(() => console.log('resolved')) // promise will resolve with response | ||
* ``` | ||
*/ | ||
export function onlyResolvesLast<T extends (...args: any) => Promise<any>>(fn: T) { | ||
let time = 0 | ||
function wrappedFn(this: ThisParameterType<T>, ...args: Parameters<T>): ReturnType<T> { | ||
const currentTime = time + 1 | ||
time = currentTime | ||
|
||
const res = fn.apply(this, args) | ||
|
||
return new Promise((resolve, reject) => { | ||
Promise.resolve(res) | ||
.then((res_) => { | ||
// just resolve last call | ||
if (currentTime === time) | ||
resolve(res_) | ||
}) | ||
.catch((err) => { | ||
// just resolve last call | ||
if (currentTime === time) | ||
reject(err) | ||
}) | ||
}) as any | ||
} | ||
|
||
return wrappedFn | ||
} |
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