-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract actionFunction into an own JS module
Several hooks will use this function. Therefore move it to an own JS module.
- Loading branch information
1 parent
c762650
commit fae226f
Showing
2 changed files
with
32 additions
and
25 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* SPDX-FileCopyrightText: 2025 Greenbone AG | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import {isDefined} from 'gmp/utils/identity'; | ||
|
||
/** | ||
* Executes a promise and handles success and error callbacks. | ||
* | ||
* @param {Promise} promise - The promise to be executed. | ||
* @param {Function} [onSuccess] - Optional callback function to be called on successful resolution of the promise. | ||
* @param {Function} [onError] - Optional callback function to be called if the promise is rejected. | ||
* @returns {Promise<*>} - The result of the onSuccess callback if provided, otherwise the resolved value of the promise. | ||
* If the promise is rejected the result of the onError callback if provided. | ||
* Otherwise the error from the rejected promise is thrown. | ||
* @throws {*} - The error from the rejected promise if onError callback is not provided. | ||
*/ | ||
export const actionFunction = async (promise, onSuccess, onError) => { | ||
try { | ||
const response = await promise; | ||
if (isDefined(onSuccess)) { | ||
return onSuccess(response); | ||
} | ||
} catch (error) { | ||
if (isDefined(onError)) { | ||
return onError(error); | ||
} | ||
throw error; | ||
} | ||
}; |