
Security News
OWASP 2025 Top 10 Adds Software Supply Chain Failures, Ranked Top Community Concern
OWASP’s 2025 Top 10 introduces Software Supply Chain Failures as a new category, reflecting rising concern over dependency and build system risks.
iterator-worker
Advanced tools
A class for running an (async or sync) iterator on the current event loop, with methods for starting and stopping its execution.
A class for running an (async or sync) iterator on the current event loop, with methods for starting and stopping its execution.
The iterator will be run in the same event loop as the calling code. This means your worker can affect the performance of the rest of your code, including causing it to block your program's execution completely.
It is advised you only use this library in situations that do not have high performance requirements, such as development environments and user scripts. Otherwise you should be running your worker in a separate thread.
import IteratorWorker from 'iterator-worker';
// Use a sleep functon to simulate waiting on i/o
const sleep = n => new Promise(resolve=>setTimeout(resolve, n));
// Example worker function
// Counts upwards, one number per second
async function* count(){
let i = 0;
while(true) {
await sleep(1000);
console.log(++i);
yield i;
}
}
// Start the worker
const worker = IteratorWorker.start(count());
// The worker is now running and will count every second.
// Sleep our code to simulate doing work.
await sleep(10000);
// Stop the worker
await worker.stop();
You can also use the class in a for-await loop, which takes care of starting and stopping the worker automatically:
for await(const {} of new IteratorWorker(count())) {
// This is equivalent to the previous code
await sleep(10000);
}
The advantage of doing this is that it will also close the iterator in case your code throws an error.
Iterator Worker
See: default
Create an instance of IteratorWorker for a given iterator.
The callback's signature is the same as general node-style callbacks:
function callback(error, result){}
In the case of an error the callback will be called with only one argument, and the worker will exit.
Otherwise, each time the iterator yields, the callback will be called with the yeilded value
as the second argument, and undefined as the first.
| Param | Type | Description |
|---|---|---|
| worker | Iterable | AsyncIterable | An iterable object |
| callback | function | A function called with each iteration result or error |
True if the worker has started
Kind: instance property of module.exports
True if the worker has finished
Kind: instance property of module.exports
BooleanStarts the iterator in the same event loop.
Kind: instance method of module.exports
Returns: Boolean - False if the iterator had been started previously, otherwise True
Throws:
IteratorWorkerError If the worker has already finished.PromiseWait for your worker to finish naturally.
If your worker uses an infinite loop, this will never resolve;
instead you should call stop
Kind: instance method of module.exports
Returns: Promise - Promise that resolves when the worker stops
PromiseStop the worker after the current iteration. and wait for it to finish.
Kind: instance method of module.exports
Returns: Promise - Promise that resolves when the worker stops
An error that occurs as part of the management of an IteratorWorker
Kind: static class of module.exports
A class representing an (async or sync) iterator, with methods for starting or stopping its execution.
The iterator will be run in the same event loop, meaning synchronous code in the worker can still block the execution of your program.
It is advised you only use this library in low-risk environments such as development environments and user scripts. Otherwise you should be running your worker in a separate thread.
Kind: static property of module.exports
Constructs and starts the iterator worker.
Has the same signature as the regular constructor,
and includes the behaviour of the start method.
Kind: static method of module.exports
FAQs
A class for running an (async or sync) iterator on the current event loop, with methods for starting and stopping its execution.
The npm package iterator-worker receives a total of 0 weekly downloads. As such, iterator-worker popularity was classified as not popular.
We found that iterator-worker demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OWASP’s 2025 Top 10 introduces Software Supply Chain Failures as a new category, reflecting rising concern over dependency and build system risks.

Research
/Security News
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.