Simple threads for Node.js
npm i like-thread
Create a thread and reuse it:
import Thread from 'like-thread'
const thread = new Thread(function (a, b) {
return a + b
})
console.log(await thread.call(1, 1)) // => 2
console.log(await thread.call(2, 2)) // => 4
await thread.close()Iterators:
const thread = new Thread(async function * (n) {
for (let i = 1; i <= n; i++) {
yield i
}
})
for await (const value of thread.call(3)) {
console.log(value) // 1, 2, 3
}
await thread.close()New temporary thread on each call:
console.log(await Thread.go([1, 1], sum)) // => 2
console.log(await Thread.go([2, 2], sum)) // => 4
function sum (a, b) {
return a + b
}MIT