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).
20 lines (19 loc) • 596 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Throttles an async iterator. Emits a value and then prevents emits until timer has completed.
*/
function throttle(timer) {
return async function* inner(source) {
let pendingTimerPromise = null;
for await (const item of source) {
if (!pendingTimerPromise) {
yield item;
pendingTimerPromise = timer(item).then(() => {
pendingTimerPromise = null;
});
}
}
};
}
exports.throttle = throttle;