opportunity
v0.1.2
Published
Action execution control via requirements and conditions
Downloads
15
Maintainers
Readme
Opportunity
Action execution control using conditional checks - Opportunities
About
Opportunity provides an asynchronous toolkit with which to manage promises and the actions they're ultimately responsible for executing. The actions and conditions provided by this library allow for the creation of complex asynchronous "stops", which are only triggered (allowing a promise chain or asynchronous procedure to continue) when all conditions for an action (opportunity) are validated (truthy).
Take a promise chain with a complex step that needs several dependencies to be in the correct state:
Opportunity provides a simple API for checking dependencies (functions, values etc.) to ensure they're all correct before continuing. Once they're all valid (at one point in time, as they're continuously checked), the action can execute:
Helper methods are provided to make checking states easier. Of course a condition can be controlled and triggered manually quite easily, but sometimes you need to check the result of an asynchronous function, and this is where the helper functions come in handy:
Usage
Let's look at a basic example:
const { createAction, whenTruthy, whileFalsy } = require("opportunity");
function someTask() {
return prefetchSomething()
.then(createAction([
whenTruthy(fetchValueFromAPI),
whileFalsy(pageIsInactive)
]))
.then(finalise);
}
Here the Promise chain returned by someTask()
is halted while the created action is inactive. Once activated, the promise will resolve and the chain will continue. The action depends on two conditions created by whenTruthy
and whileFalsy
. These are helpers, and they check functions or promises for their return value. Once a value is returned (either synchronously or asynchronously), the state of the condition is updated. If all conditions are in the true
(active) state, the action will be triggered.