@andreabiagini5/applicazioni-e-servizi-web-project
Version:
Project for Applicazioni e Servizi Web.
25 lines • 1.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Asynchronously finds the first element in the array that satisfies the provided asynchronous predicate function.
*
* The function executes the predicate sequentially for each element in the array, awaiting the result each time.
* Once a predicate returns `true`, the corresponding element is immediately returned.
* If no element satisfies the predicate, `undefined` is returned.
*
* @template T - The type of elements in the array.
* @param predicate - An asynchronous function that takes the current element, its index, and the array, and returns a Promise<boolean>.
* @returns A Promise that resolves to the first element that satisfies the predicate, or `undefined` if none do.
*/
const findAsync = async function (predicate) {
for (let i = 0; i < this.length; i++) {
if (await predicate(this[i], i, this)) {
return this[i];
}
}
return undefined;
};
if (!Array.prototype.findAsync) {
Array.prototype.findAsync = findAsync;
}
//# sourceMappingURL=array.extensions.js.map