Attask
An approach to conditional promise chains with state and providers.
const Attask = ; Attask ;
The above example will output [0, 1, 2, 4, 10]
Getting Started
npm install --save attask
Attask can be instantiated in a couple of ways but the simplest way to create a new chain with the static methods async or sync
Attask.async() tells attask to execute the task chains in parallel -
Attask ;;
Output
- 0ms: B
- 0ms: C
- 2.5ms: A
- 2.5ms: DONE
Attask.sync() in contrast, will guarantee execute the tasks sequentially -
Attask ;;
Output
- 2.5ms: A
- 2.5ms: B
- 2.5ms: C
- 2.5ms: DONE
Must, Might or Wont
In order to populate chains with actions you use the methods must, might, or wont.
- Tasks added with must will abort all other tasks and pending tasks on failure
- Tasks added with wont must fail and will abort all other tasks and pending tasks on success
- Tasks added with might will not cancel other tasks when failed
Tasks in must and might and wont groupings will execute in parallel even with the sync flag.
Attask
Tasks
Tasks provided can be one of two things.
A function that accepts the provider and store. Can return a promise for async execution.
{ //...} //...
A class that implements the run method that accepts the provider and store. This can also return a promise
{ //... } //...
Provider
Attask chains can be provided an attachment for the current work load. This attachment will be used as the first argument for a task callback.
const attask = Attask; // An empty array is the provider attask; attask; attask; //Will log "[0]"
State
Attask chains can be passed a state that is then abstracted in to a store object. This store object will be used as the second argument for a task callback
Attask ;
Here's a list of the methods available on the store -
resolvekey:string:T; haskey:string:boolean; setkey:string, entry:any:AttaskStore; removekey:string:AttaskStore; keys:string; collect...key: string: T; hasAny...keys: string: boolean; hasNone...keys: string: boolean; hasAll...keys: string: boolean; haskey: string: boolean; resolvekey: string: T; getkey: string, ifNotFound?: T: ; any...keys: any: AttaskOptional<T>; all...keys: any: AttaskOptional<T>; absorbsource: AttaskStore | any: void;
Note, you can also store an object that implements the first 5 methods above. Attask will see the object's signature and expand the task store with that object's elements.
Optionals
AttaskOptional objects returned from any or all have the following methods
is...resolution:booleanexists:booleangetifNotFound:T = null:T
Chaining
To append another attask chain use the after method
Attask //A brand new attask chain
By default a child chain will not use the provider available in the parent or the store available in the parent, when changed.
In order to keep this connection alive you can use the link directive to let attask know that the child chain relies on the parent provider even when changed.
const parent = Attask;const child = parent; childlink; //Will inherit any configuration changes parent; //child will now have this provider because it was linked
Errors
Use catch to use your own error handler, by default attask's error handler is a console.error call.
Attask
Silence
In order to disable error handling without a noop function for catch, you can simply use the silence directive
Attask ;
All methods available on Attask (@see attask.d.ts)
sync: Attask<P>; static sync: Attask<P>;
If you got this far, you can tell this documentation needs some work.
Please feel free to fork and contribute in any way possible.