forest-promises
Forest Promises: create and track dependency forests of promises.
Documentation
Forest
Comming soon.
Tree
Constructor
let tree = promise name = 'task' track = 0 ident = 0
A Tree
wraps a Promise
. It has a state variable that tracks its wrapped promise state.
// Initial state is 'pending'.thisstate = STATESPENDING // Wraps the promise and tracks its state.thispromise = promise
It also has a name
and a ident
, mainly used to display a tree's status string. The track
paramenter enables, if greater than zero, a log of the tree's status string every track
milliseconds.
Static methods
Tree.STATES
is a getter to a static constant enumerate object that contains all possible states of a Tree
instance: pending, fulfilled and rejected.
Instance methods
The Tree
class wraps its promise instance methods then
, catch
and finally
. It also provides a status_str
getter that serializes the tree instance to a idented human friendly string that displays its state
and name
. The resolved
method returns wheter the tree's promise is resolved.
The log
method writes the tree's status string to the issued logger. The track
method writes it until the tree state is resolved.
Examples
The following code showcase the stand alone usage of Trees.
let toggle = false let { } executor '1. fulfilled' 50 executor '2. rejected' 50 executor '3. not tracked' let tree = executor '4. rejected' 50tree
The executor
function is used to create promises. It alternates between resolve
and reject
calls, so that odd trees will be fulfilled and even trees will be rejected. Trees 1, 2 and 4 are tracked every 50 milliseconds. Tree number 3 is not tracked.
Output
◆ 1. fulfilled
◆ 2. rejected
◆ 4. rejected
i 2. regected --> a tree was rejected.
i the tree "4. rejected" was rejected.
✔ 1. fulfilled
✘ 2. rejected
✘ 4. rejected
Credits
Image credit. Literally inspired the creation of this module.