UNPKG

ix

Version:

The Interactive Extensions for JavaScript

44 lines (42 loc) 1.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.find = void 0; const tslib_1 = require("tslib"); const withabort_js_1 = require("./operators/withabort.js"); const aborterror_js_1 = require("../aborterror.js"); /** * Returns the value of the first element in the provided async-iterable that satisfies the provided testing function. * * @template T The type of the elements in the source sequence. * @param {AsyncIterable<T>} source An async-iterable sequence whose elements to apply the predicate to. * @param {FindOptions<T>} options The options for a predicate for filtering, thisArg for binding and AbortSignal for cancellation. * @returns {(Promise<S | undefined>)} A promise with the value of the first element that matches the predicate. */ function find(source, options) { var _a, e_1, _b, _c; return tslib_1.__awaiter(this, void 0, void 0, function* () { const { ['signal']: signal, ['thisArg']: thisArg, ['predicate']: predicate } = options; (0, aborterror_js_1.throwIfAborted)(signal); let i = 0; try { for (var _d = true, _e = tslib_1.__asyncValues((0, withabort_js_1.wrapWithAbort)(source, signal)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const item = _c; if (yield predicate.call(thisArg, item, i++, signal)) { return item; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_1) throw e_1.error; } } return undefined; }); } exports.find = find; //# sourceMappingURL=find.js.map