ix
Version:
The Interactive Extensions for JavaScript
41 lines (39 loc) • 1.78 kB
JavaScript
import { __asyncValues, __awaiter } from "tslib";
import { wrapWithAbort } from './operators/withabort.mjs';
import { throwIfAborted } from '../aborterror.mjs';
/**
* Returns the a Promise containing the index of the first element in the array that satisfies the provided testing function.
* Otherwise, it returns a Promise with -1, indicating that no element passed the test.
*
* @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<number>} A promise containing the index of the first element in the array that passes the test. Otherwise, -1.
*/
export function findIndex(source, options) {
var _a, e_1, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
const { ['signal']: signal, ['thisArg']: thisArg, ['predicate']: predicate } = options;
throwIfAborted(signal);
let i = 0;
try {
for (var _d = true, _e = __asyncValues(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 i;
}
}
}
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 -1;
});
}
//# sourceMappingURL=findindex.mjs.map