Task(nameopt, fnopt, depsopt)
A task that is callable as a function.
A called task returns a Promise.
Constructor
new Task(nameopt, fnopt, depsopt)
Parameters:
| Name | Type | Attributes | Description | 
|---|---|---|---|
name | 
            
            string | 
                
                    <optional> | 
            
            
            Task name  | 
        
fn | 
            
            function | 
                
                    <optional> | 
            
            
            Task job  | 
        
deps | 
            
            Array.<Dependee> | 
                
                    <optional> | 
            
            
            Task dependencies  | 
        
Examples
var task = new Task('greet', () => { console.log('Hello!') });
task(); // "Hello!"
    
    var task = new Task('notify', resolve => {
  setTimeout(() => {
    console.warn('Hey!');
    resolve('task done.');
  }, 3000);
});
task() // runs asnychronously, and returns a promise
.then(resol => { // runs after 3 seconds
  console.log(resol); // "task done."
});
    
    var taskA = new Task('A', resolve => {
  setTimeout(() => { console.log('task A done.'); resolve() }, 3000);
});
var taskB = new Task('B', resolve => {
  setTimeout(() => { console.log('task B done.'); resolve() }, 6000);
});
var taskC = new Task('C', () => {
  console.log('task C done.');
}, [taskA, taskB]); // dependencies
taskC(); // outputs:
// "task A done."
// "task B done."
// "task C done."
// NOTE: taskA and taskB run in parallel
    
    new Task('task B', [taskA]);      // name and dependencies (no job)
new Task(() => { ... }, [taskA]); // job and dependencies (no name)
new Task(() => { ... });          // only job (no name, no dependencies)
new Task([taskA]);                // only dependencies (no name, no job)
    
    var taskA = new Task('task A', () => { ... });
var taskB = new Task('task B', () => { ... });
var taskC = new Task('task C', () => { ... });
taskC.depend(taskA, taskB);       // add taskA and taskB as dependencies
taskC();
    
    var task = new Task((resolve, reject) => {
  throw 'I AM ERROR';
  // or
  reject('I AM ERROR');
});
task().catch(e => {
  // NOTE: e is a TaskJobFailure instance
  console.error(e.info.thrown); // 'I AM ERROR'
});
    
    Members
staticreadonlyDependency class
TaskDependency class
staticreadonlyDepFailure class
TaskDepFailure class
staticreadonlyException class
TaskException class
staticreadonlyJobFailure class
TaskJobFailure class
staticreadonlyManager class
TaskManager class
console conso1e
The console object to ouput the logs
readonlydisplayName string
Name of this task
readonlyhasDep boolean
Whether this task has any dependency
readonlyhasName boolean
Whether this task has name
readonlyisBusy boolean
Whether this task is busy
readonlyisDone boolean
Whether this task is done
readonlyisFailed boolean
Whether this task is failed
readonlyisIdle boolean
Whether this task is idle
readonlyisRegistered boolean
Whether this task is registered for a TaskManager
readonlylogLevel integer
The log threshold
readonlymanager TaskManagernull
The manager that this task has registered for
readonlyresol any
The resolution value
readonlystate string
The current state of this task
| Possible State | Meaning | 
|---|---|
IDLE | 
The initial state | 
BUSY | 
Resolving | 
DONE | 
Resolved | 
FAILED | 
Failed to resolve | 
Methods
staticoption(name, valueopt) → anyclass
Returns the specified option value, or assigns a new value to it.
Available Options:
| Name | Default Value | Description | 
|---|---|---|
defaultManager | 
The global TaskManager instance | 
The default TaskManager | 
defaultConsole | 
The global console | 
The default console to output logs | 
defaultLogLevel | 
'WARNING' | 
The default log level | 
colorSupport | 
CLI: 1, browser: 0 | 
The default color support level for the console | 
Parameters:
| Name | Type | Attributes | Description | 
|---|---|---|---|
name | 
            
            string | Option name  | 
        |
value | 
            
            any | 
                
                    <optional> | 
            
            
            
Returns:
the option value, or Task class if the 2nd argument was provided
- Type:
 - any | class
 
staticoptions(optsopt) → objectclass
Returns all the option values in a plain object form.
If an object is provided as the argument, assigns the each property value to the corresponding option.
Parameters:
| Name | Type | Attributes | Default | Description | 
|---|---|---|---|---|
opts | 
            
            object | 
                
                    <optional> | 
            
            
                null | Name-value pairs to assign  | 
        
Returns:
the options as an object, or Task class if the argument was provided
- Type:
 - object | class
 
staticreset() → class
Resets all the options to the initial values.
Returns:
the Task class
- Type:
 - class
 
dep(key) → any
Finds and returns the resolution of a dependency.
Parameters:
| Name | Type | Description | 
|---|---|---|
key | 
            
            number | string | Index or name of a dependency  | 
        
Returns:
the resolution of the dependency
- Type:
 - any
 
depend(…deps) → Task
Adds one or more dependencies.
Parameters:
| Name | Type | Attributes | Description | 
|---|---|---|---|
deps | 
            
            Dependee | 
                
                
                
                    <repeatable> | 
            
            
            One or more dependencies to add.  | 
        
Throws:
an error if this task is not idle
Returns:
this object
- Type:
 - Task
 
deregister() → Task
Deregisters this task from a TaskManager.
Returns:
this object
- Type:
 - Task
 
error(…msg) → Task
Logs msg as an error.
Parameters:
| Name | Type | Attributes | Description | 
|---|---|---|---|
msg | 
            
            any | 
                
                
                
                    <repeatable> | 
            
            
            Any type of value to log  | 
        
Returns:
this object
- Type:
 - Task
 
log(…msg) → Task
Logs a message, object, or any kind of value.
Parameters:
| Name | Type | Attributes | Description | 
|---|---|---|---|
msg | 
            
            any | 
                
                
                
                    <repeatable> | 
            
            
            Any type of value to log  | 
        
Returns:
this object
- Type:
 - Task
 
register(manageropt) → Task
Registers this task for a TaskManager.
Parameters:
| Name | Type | Attributes | Default | Description | 
|---|---|---|---|---|
manager | 
            
            TaskManager | 
                
                    <optional> | 
            
            
                null | The   | 
        
Returns:
this object
- Type:
 - Task
 
run() → Promise
Calls this task as a function.
That means task.run() is same as task()
Returns:
- Type:
 - Promise
 
setLogLevel(level) → Task
Sets a log threshold.
Parameters:
| Name | Type | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
level | 
            
            integer | string | Log level in an integer or a string form (recommended). 
  | 
        
Returns:
this object
- Type:
 - Task
 
warn(…msg) → Task
Logs msg as a warning.
Parameters:
| Name | Type | Attributes | Description | 
|---|---|---|---|
msg | 
            
            any | 
                
                
                
                    <repeatable> | 
            
            
            Any type of value to log  | 
        
Returns:
this object
- Type:
 - Task