@solid/community-server
Version:
Community Solid Server: an open and modular implementation of the Solid specifications
50 lines • 1.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isPromise = isPromise;
exports.resolvePromiseOrValue = resolvePromiseOrValue;
exports.promiseSome = promiseSome;
const node_util_1 = require("node:util");
/**
* Verifies if the given value is a Promise or not.
*
* @param object - Object to check.
*/
function isPromise(object) {
return node_util_1.types.isPromise(object);
}
/**
* Calls `callback` with the resolved value of `object`.
* In case `object` is a Promise, the result will also be a Promise,
* otherwise the result will be sync.
*/
function resolvePromiseOrValue(object, callback) {
if (isPromise(object)) {
return object.then((val) => callback(val));
}
return callback(object);
}
function noop() { }
/**
* A function that simulates the Array.some behaviour but on an array of Promises.
* Returns true if at least one promise returns true.
* Returns false if all promises return false or error.
*
* @remarks
*
* Predicates provided as input must be implemented considering
* the following points:
* 1. if they throw an error, it won't be propagated;
* 2. throwing an error should be logically equivalent to returning false.
*/
async function promiseSome(predicates) {
return new Promise((resolve) => {
function resolveIfTrue(value) {
if (value) {
resolve(true);
}
}
Promise.all(predicates.map(async (predicate) => predicate.then(resolveIfTrue, noop)))
.then(() => resolve(false), noop);
});
}
//# sourceMappingURL=PromiseUtil.js.map