-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
0e6850f
commit 55bd3bb
Showing
5 changed files
with
140 additions
and
0 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,4 +1,5 @@ | ||
export * from './never'; | ||
export * from './revert'; | ||
export * from './try-catch'; | ||
export * from './try-one-by-one'; | ||
export * from './timeout'; |
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,30 @@ | ||
import { tryOneByOne } from '../try-one-by-one'; | ||
|
||
declare global { | ||
export interface PromiseConstructor { | ||
/** | ||
* #### Try One By One | ||
* Tries a set of promises one by one with given order. Breaks the call when a promise resolved. Otherwise keeps trying incoming promises until the list is finished. | ||
* | ||
* * * * | ||
* Example: | ||
* ```typescript | ||
* import "@thalesrc/js-utils/promise/static/try-one-by-one"; | ||
* | ||
* async function fooFunction() { | ||
* const foo = await Promise.tryOneByOne([ | ||
* () => someCall(), | ||
* (err) => anotherCall(), | ||
* (err) => fooPromise() | ||
* ]); | ||
* | ||
* // do stuff | ||
* } | ||
* ``` | ||
* * * * | ||
*/ | ||
tryOneByOne: typeof tryOneByOne; | ||
} | ||
} | ||
|
||
Promise.tryOneByOne = tryOneByOne; |
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,34 @@ | ||
import 'jest'; | ||
|
||
import { tryOneByOne } from './try-one-by-one'; | ||
|
||
class CustomError { | ||
constructor(public message: string) {} | ||
} | ||
|
||
describe('TryOneByOne Function', () => { | ||
it('should resolve if a promise resolved', done => { | ||
tryOneByOne([Promise.resolve('foo')]) | ||
.then(res => { | ||
expect(res).toBe('foo'); | ||
done(); | ||
}) | ||
.catch(err => { | ||
throw new CustomError("didn't resolve"); | ||
}); | ||
}); | ||
|
||
it('should try the next promise when the first threw error', done => { | ||
tryOneByOne([ | ||
Promise.reject('foo'), | ||
Promise.resolve('bar'), | ||
]) | ||
.then(res => { | ||
expect(res).toBe('bar'); | ||
done(); | ||
}) | ||
.catch(err => { | ||
throw new CustomError("didn't resolve"); | ||
}); | ||
}); | ||
}); |
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,58 @@ | ||
/** | ||
* #### Try One By One | ||
* | ||
* Tries a set of promises one by one with given order. Breaks the call when a promise resolved. Otherwise keeps trying incoming promises until the list is finished. | ||
* | ||
* * * * | ||
* Example: | ||
* ```typescript | ||
* import { tryOneByOne } from "@thalesrc/js-utils/promise"; | ||
* | ||
* async function fooFunction() { | ||
* const foo = await tryOneByOne([ | ||
* () => someCall(), | ||
* (err) => anotherCall(), | ||
* (err) => fooPromise() | ||
* ]); | ||
* | ||
* // do stuff | ||
* } | ||
* | ||
* ``` | ||
* | ||
* Static Example: | ||
* ```typescript | ||
* import "@thalesrc/js-utils/promise/static/try-one-by-one"; | ||
* | ||
* async function fooFunction() { | ||
* const foo = await Promise.tryOneByOne([ | ||
* () => someCall(), | ||
* (err) => anotherCall(), | ||
* (err) => fooPromise() | ||
* ]); | ||
* | ||
* // do stuff | ||
* } | ||
* | ||
* ``` | ||
* * * * | ||
* @param promises List of promises to try one by one with the order | ||
* @returns | ||
*/ | ||
export async function tryOneByOne<T>(promises: Array<Promise<T> | ((lastError: unknown) => Promise<T>)>): Promise<T> { | ||
return await tryPromises([...promises], null); | ||
} | ||
|
||
async function tryPromises<T>(promises: Array<Promise<T> | ((lastError: unknown) => Promise<T>)>, lastError: unknown): Promise<T> { | ||
const promiseFn = promises.shift(); | ||
|
||
if (!promiseFn) throw lastError; | ||
|
||
try { | ||
const promise = typeof promiseFn === "function" ? promiseFn(lastError) : promiseFn; | ||
|
||
return await promise; | ||
} catch (err) { | ||
return tryPromises(promises, err); | ||
} | ||
} |