requiret

1.0.4 • Public • Published

requiret (Require Threads)

Require a module as a Worker Thread in Node.js

Requirements

  • Node.js 10.5.0+

Install

npm install requiret

Demo (converting a sync function to an async function)

readfile.js (this is our thread)

const fs = require('fs')
 
//It must exports an async function
module.exports = async function(file){
    const data = fs.readFileSync(file, 'utf-8')
    return data
}

index.js

const requiret = require('requiret')
const readfile = requiret('./readfile.js')
 
readfile('test.txt')
.then(text => {
    console.log(text)
})
.catch(error => {
    console.error('An error has ocurred:', error)
})

Running

You must run node with the --experimental-worker flag enabled.

node --experimental-worker index.js

Package Sidebar

Install

npm i requiret

Weekly Downloads

5

Version

1.0.4

License

ISC

Unpacked Size

2.02 kB

Total Files

3

Last publish

Collaborators

  • samuelnovaes