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
.
npm install async-task
Also support bower
bower install async-task
var AsyncTask = require( 'async-task' )
var task = new AsyncTask((a, b) => a + b)
task.execute(1, 2)
.then(function( result ) {
result === 3
})
.catch( handleException )
Creates a new AsyncTask
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
Execute the doInBackground
function with supplied args.
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()
})
npm run-script test
doInBackground
can return a promise or maybe even agenerator*
so you can iterate overasyncTask.execute
If your using it in your very cool project please drop me a note on jornandretangen àt
gmail.com
Partially made, with <3 at: