https://github.com/yornaath/async-task
Execute tasks asynchronous tasks on web Worker without seperate files.
https://github.com/yornaath/async-task
Last synced: about 1 month ago
JSON representation
Execute tasks asynchronous tasks on web Worker without seperate files.
- Host: GitHub
- URL: https://github.com/yornaath/async-task
- Owner: yornaath
- Created: 2014-02-21T22:37:29.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2018-03-29T16:15:33.000Z (over 7 years ago)
- Last Synced: 2025-08-19T05:57:21.875Z (about 2 months ago)
- Language: JavaScript
- Homepage: http://gorillatron.github.io/async-task/
- Size: 502 KB
- Stars: 170
- Watchers: 4
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
AsyncTask [](https://travis-ci.org/gorillatron/async-task) [](https://codeclimate.com/github/gorillatron/async-task)
=========
Execute tasks asynchronous tasks without seperate files. In browsers without ```Worker``` support it fallbacks to ```iframe```.In Nodejs it spawns a process using ```child_process```.
### Install
```
npm install async-task
```Also support bower
```
bower install async-task
```### Usage
```javascript
var AsyncTask = require( 'async-task' )var task = new AsyncTask((a, b) => a + b)
task.execute(1, 2)
.then(function( result ) {
result === 3
})
.catch( handleException )
```### API
#### AsyncTask( doInBackground, options )
Creates a new AsyncTask
##### options
* ```options.keepAlive``` Keep worker alive so ```.execute``` can be called multiple times.
* ```options.worker``` Supply worker if you want to share worker between tasks. **NB!: termination of worker is left to the user**#### asyncTask.execute( args... ):Promise
Execute the ```doInBackground``` function with supplied args.
###### Sharing worker example
```javascript
var AsyncTask = require( 'async-task' )
var BackgroundWorker = require( 'background-worker' )var worker = new BackgroundWorker({})
var taskA = new AsyncTask(() => 'a', {worker})
var taskB = new AsyncTask(() => 'b', {worker})
Promise.all([
taskA.execute(),
taskB.execute()
]).then(function(result) {
result == [ 'a', 'b' ]
worker.terminate()
})
```#### Test
```npm run-script test```
### Roadmap
* ```doInBackground``` can return a promise or maybe even a ```generator*``` so you can iterate over ```asyncTask.execute```
### In the wild
If your using it in your very cool project please drop me a note on jornandretangen ```àt``` gmail.com
*Partially made, with <3 at:*
[](https://github.com/wtw-software/)