https://github.com/samuelnovaes/requiret
Require module as Worker Thread in Node.js
https://github.com/samuelnovaes/requiret
Last synced: 3 months ago
JSON representation
Require module as Worker Thread in Node.js
- Host: GitHub
- URL: https://github.com/samuelnovaes/requiret
- Owner: samuelnovaes
- Created: 2018-07-03T15:19:57.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-07-07T21:55:11.000Z (almost 7 years ago)
- Last Synced: 2025-02-14T21:48:02.378Z (3 months ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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)
```javascript
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
```javascript
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
```