Skip to content

LuKks/like-thread

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

like-thread

Simple threads for Node.js

npm i like-thread

Usage

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
}

License

MIT

About

Simple threads for Node.js

Resources

License

Stars

Watchers

Forks

Packages

No packages published