axax
Version:
A library of async iterator extensions for JavaScript including ```map```, ```reduce```, ```filter```, ```flatMap```, ```pipe``` and [more](https://github.com/jamiemccrindle/axax/blob/master/docs/API.md#functions).
31 lines (30 loc) • 880 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/** Provides a way for callbacks to signal early completion */
class StopError extends Error {
}
exports.StopError = StopError;
/**
* Converts an async iterable into a series of callbacks. The function returns
* a promise that resolves when the stream is done
*
* @param callback the callback that gets called for each value
*/
function toCallbacks(callback) {
return async function inner(source) {
const iterator = source[Symbol.asyncIterator]();
while (true) {
const result = await iterator.next();
try {
await callback(result);
}
catch (StopError) {
return;
}
if (result.done) {
return;
}
}
};
}
exports.toCallbacks = toCallbacks;