-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'fix/#5098/amqp2tty-create-event-delay' into 'develop'
[Issue #5098] introduce promisedWait for async handling and refactor event actions See merge request canopsis/canopsis-pro!4279
- Loading branch information
Showing
6 changed files
with
94 additions
and
10 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
67 changes: 67 additions & 0 deletions
67
community/sources/webcore/src/canopsis-next/src/helpers/async.js
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,67 @@ | ||
/** | ||
* Executes a callback function after a specified timeout and returns a promise that resolves with | ||
* the callback's result. | ||
* | ||
* @function | ||
* @param {Function} callback - The asynchronous function to be executed after the timeout. It should return a promise | ||
* or a value. | ||
* @param {number} [timeout=0] - The time, in milliseconds, to wait before executing the callback. | ||
* @returns {Promise<*>} A promise that resolves with the result of the callback function. | ||
* | ||
* @example | ||
* // Example 1: Using promisesTimeout with a simple callback | ||
* promisesTimeout(() => 'Hello, World!', 1000).then(result => { | ||
* console.log(result); // Logs 'Hello, World!' after 1 second | ||
* }); | ||
* | ||
* @example | ||
* // Example 2: Using promisesTimeout with an asynchronous callback | ||
* const asyncFunction = async () => { | ||
* return new Promise(resolve => { | ||
* setTimeout(() => { | ||
* resolve('Async Result'); | ||
* }, 500); | ||
* }); | ||
* }; | ||
* | ||
* promisedTimeout(asyncFunction, 1000).then(result => { | ||
* console.log(result); // Logs 'Async Result' after 1 second | ||
* }); | ||
* | ||
* @example | ||
* // Example 3: Using promisedTimeout with default timeout | ||
* promisesTimeout(() => 'Immediate Result').then(result => { | ||
* console.log(result); // Logs 'Immediate Result' immediately | ||
* }); | ||
*/ | ||
export const promisedTimeout = (callback, timeout = 0) => new Promise(resolve => setTimeout(async () => { | ||
const result = await callback?.(); | ||
|
||
return resolve(result); | ||
}, timeout)); | ||
|
||
/** | ||
* Waits for a specified timeout before resolving a promise. | ||
* This function is useful for introducing delays in asynchronous workflows. | ||
* | ||
* @function | ||
* @param {number} timeout - The time, in milliseconds, to wait before the promise resolves. | ||
* @returns {Promise<void>} A promise that resolves after the specified timeout. | ||
* | ||
* @example | ||
* // Example 1: Using promisedWait to introduce a delay | ||
* promisedWait(2000).then(() => { | ||
* console.log('Waited for 2 seconds'); | ||
* }); | ||
* | ||
* @example | ||
* // Example 2: Using promisedWait in an async function | ||
* async function delayedExecution() { | ||
* console.log('Start delay'); | ||
* await promisedWait(1000); | ||
* console.log('End delay after 1 second'); | ||
* } | ||
* | ||
* delayedExecution(); | ||
*/ | ||
export const promisedWait = timeout => promisedTimeout(() => {}, 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
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