https://github.com/unlight/deasynchronize
Run asynchronous or synchronous function in subprocess and return result synchronously
https://github.com/unlight/deasynchronize
deasync deasynchronize subprocess
Last synced: 12 months ago
JSON representation
Run asynchronous or synchronous function in subprocess and return result synchronously
- Host: GitHub
- URL: https://github.com/unlight/deasynchronize
- Owner: unlight
- License: mit
- Created: 2020-02-08T14:16:30.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-27T15:48:21.000Z (almost 6 years ago)
- Last Synced: 2024-04-25T13:02:14.902Z (about 2 years ago)
- Topics: deasync, deasynchronize, subprocess
- Language: JavaScript
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# deasynchronize
Run asynchronous or synchronous function in subprocess and return result synchronously
## Install
```sh
npm i -S deasynchronize
```
## Usage
```ts
const result = deasynchronize(1, 2, async (a, b) => {
// This is not a closure!
await new Promise(resolve => setTimeout(resolve, 0));
return `async result ${a + b}`;
});
console.log(result); // => 'async result 3'
```
## API
```ts
deasynchronize(...args: any[], f: (...args: any[]) => any): string
```
Function `f` is not a closure and do not has access to parent scope, it runs in subprocess.
You should require all dependencies inside this function. Result will be converted to string.
```ts
const result = deasynchronize(async () => {
// This is not a closure!
const os = require('os');
return os.platform();
});
```
It is possible to pass parameters to function:
```ts
const result = deasynchronize('meow', async sound => {
// This is not a closure!
return `Incoming sound is ${sound}`;
});
console.log(result); // => 'Incoming sound is meow'
```
Remember, this function is not a closure!
## Similar Packages
- https://github.com/ForbesLindesay/sync-rpc