@johngw/stream
Version:
Reactive programming tools using the WHATWG Streams API.
33 lines • 833 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.interpose = void 0;
/**
* Hold up a stream until a promise resolves. If it errors,
* the error will be emitted to the stream.
*
* @group Transformers
* @example
* ```
* ----a-------b------|
*
* interpose(async () => await setTimeout(50))
*
* --------a--------b-|
* ```
*/
function interpose(promise) {
const fn = typeof promise === 'function' ? promise : () => promise;
return new TransformStream({
async transform(chunk, controller) {
try {
await fn(chunk);
}
catch (error) {
return controller.error(error);
}
controller.enqueue(chunk);
},
});
}
exports.interpose = interpose;
//# sourceMappingURL=interpose.js.map