-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(invoices): moved waitFor util to test dir
- Loading branch information
1 parent
0e9edd8
commit dc524b7
Showing
3 changed files
with
28 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* This helper keeps polling a condition function until it returns a value or times out. | ||
* Use this instead of arbitrary `await new Promise()` calls. | ||
* | ||
* @example | ||
* // This invoice will be defined, or an error is thrown after timeout | ||
* const invoice = await waitFor(() => getInvoice(order.id)); | ||
*/ | ||
export async function waitFor<T>( | ||
conditionFn: () => Promise<T | undefined> | T | undefined, | ||
interval = 100, | ||
timeout = 10000 | ||
) { | ||
const startTime = Date.now(); | ||
let result: T | undefined; | ||
let elapsedTime = 0; | ||
while (elapsedTime < timeout) { | ||
result = await conditionFn(); | ||
if (result) { | ||
return result; | ||
} | ||
elapsedTime = Date.now() - startTime; | ||
await new Promise((resolve) => setTimeout(resolve, interval)); | ||
} | ||
throw new Error(`'waitFor()' Failed to resolve a value after ${timeout}ms`); | ||
} |
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
31 changes: 1 addition & 30 deletions
31
packages/vendure-plugin-shipping-extensions/test/test-helpers.ts
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