-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handler-fetch: improve the way to work with the worker
- Loading branch information
1 parent
563c445
commit 9b404f2
Showing
4 changed files
with
107 additions
and
77 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
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,28 @@ | ||
// @ts-check | ||
|
||
/** | ||
* Wait for a variable to be truthy, with a timeout. | ||
* | ||
* @param {Function} getValueFunction A function that needs to return a truthy value to resolve the promise | ||
* @param {number} [timeoutMs] The maximum time to wait for the variable to be truthy, in milliseconds | ||
* @param {number} [checkIntervalMs] The interval at which to check the variable's value, in milliseconds | ||
* @param {string} [errorMessage] The error message to use if the promise is rejected | ||
* @returns {Promise<void>} | ||
*/ | ||
export const waitForVariableToBeTrue = async (getValueFunction, timeoutMs = 30000, checkIntervalMs = 20, errorMessage = 'Reached Timeout') => { | ||
return new Promise((resolve, reject) => { | ||
// Check the variable's value periodically | ||
const interval = setInterval(() => { | ||
if (getValueFunction()) { | ||
clearInterval(interval) | ||
resolve() | ||
} | ||
}, checkIntervalMs) | ||
|
||
// Set a timeout to reject the promise if the time exceeds the specified duration | ||
setTimeout(() => { | ||
clearInterval(interval) | ||
reject(new Error(errorMessage)) | ||
}, timeoutMs) | ||
}) | ||
} |
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