https://github.com/samthor/syncingabout
https://github.com/samthor/syncingabout
Last synced: 12 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/samthor/syncingabout
- Owner: samthor
- License: apache-2.0
- Created: 2021-07-11T01:19:45.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-08-28T05:05:31.000Z (almost 5 years ago)
- Last Synced: 2025-03-12T05:29:46.939Z (over 1 year ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Converts async methods to sync using `worker_threads`.
Library related to [this blog post](https://whistlr.info/2021/block-nodejs-main-thread/).
Does not require compilation steps to work, can run anywhere in Node.js, has zero dependencies.
This'll allow you to call async methods _as if_ they were sync, but you have to move the async methods into their own file (and they'll run inside a `Worker`).
# Usage
Currently ESM for now, but could be made CJS.
Here's a demo which artificially loads a file using the async API of the built-in FS library, but makes those calls sync to the main thread.
In your main file:
```js
// main.js
import build from 'syncingabout';
const method = build('./method.js'); // relative to cwd, not this file
const result = method('foo.json');
console.info('did something sync!', result);
```
In your helper file (called "method.js" here), do this:
```js
// method.js
import {promises as fsPromises} from 'fs';
export default async function(filename) {
// do something async just for fun
await new Promise((r) => setTimeout(r, 1000));
const data = await fs.readFile(filename);
return data;
};
```
Great! 🥳