dino-core
Version:
A dependency injection framework for NodeJS applications
44 lines • 1.56 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.PromiseHelper = void 0;
const object_helper_1 = require("./object.helper");
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
class PromiseHelper {
/**
* Validate that the provided object is a promise or not
*
* @param {any} obj the object to be validated as a promise
*
* @static
* @public
*/
static isAPromise(obj) {
return object_helper_1.ObjectHelper.isDefined(obj) && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
}
/**
* Allows to wait for a promise to be resolved or invoke the provided directly if the value is not a promise.
* If the provided value is undefined no action will be taken
*
* @param {Promise | any} value the value to check and process
* @param {Function} callback the callback to invoke when the value is processed,
* the callback will accept a parameter that is the value or the value resolved from the promise
* @returns void
*
* @static
* @public
*/
static handleAsPromiseIfRequired(value, callback) {
if (value !== undefined) {
if (PromiseHelper.isAPromise(value)) {
return value.then((cd) => {
callback(cd);
});
}
else {
callback(value);
}
}
}
}
exports.PromiseHelper = PromiseHelper;
//# sourceMappingURL=promise.helper.js.map
;