andywer / andywer/threads.js

Support (async) generator functions

Open
#251 2 comments 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
3.5k
Forks
173
PR merge metrics
No merged PRs in 30d

Description

I think it would be great if the observer pattern described [here](https://threads.js.org/usage-observables) can be written as follows so that we do not have to depend on any observable libraries but ECMAScript standards.

```js
// master.js
import { spawn, Thread, Worker } from "threads"

const counter = await spawn(new Worker("./workers/counter"))

for await (const newCount of counter()) {
console.log(`Counter incremented to:`, newCount)
}
```

```js
// workers/counter.js
import { expose } from "threads/worker"

function* startCounting() { // async generator function should also be OK
for (let currentCount = 1; currentCount <= 10; currentCount++) {
yield currentCount;
}
}

expose(startCounting)
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.