better-flow v1.0.1
Better Flow
Better Flow is made to replace Promises and Callbacks.
Why?
Promises use setTimeout and have performance issues. Callbacks have bad syntax.
Examples
with callbacks:
newFlow(params, (err, result) => {
if (err) {...}
doSomeThing(params, (err, result2) => {
if (err) {...}
nextFlow(params, (err, result3) => {
// done
});
});
});
with Promise Hell:
newFlow(params).then((result) => {
doSomeThing(params).then((result2) => {
nextFlow(params).then((result3) => {
// done
}, (err) => {});
}, (err) => {});
}, (err) => {});
with Promises:
newFlow(params).then((result) => {
return doSomeThing(params);
}, (err) => {})
.then((result2) => {
return nextFlow(params);
}, (err) => {})
.then((result3) => {
// done
}, (err) => {});
with Better Flow:
const betterFlow = require('better-flow');
const flow = new betterFlow();
flow.on('newFlowDone', doSomeThing, params);
flow.on('doSomeThingReady', nextFlow, params);
flow.on('nextFlowFinish', finishFlow, params);
flow.catch(allErrorsHere);
flow.alert(allNotifiesHere);
newFlow(params);
// functions
function newFlow(params) {
if (...) {
flow.resolve('newFlowDone', newParams);
} else {
flow.reject('newFlowDone', newParams);
}
// OR
flow.current = 'newFlowDone';
if (...) {
flow.resolve(newParams);
} else {
flow.reject(newParams);
}
}
function doSomeThing(params) {
if (params.error) {
// rejected
}
if (params.result) {
// resolved
}
if (params.notify) {
// notified
}
}
function nextFlow(params) {
// i don't care about errors & notify or resolve parameters
// if we have flow.catch and flow.alert - i'm called only from resolve :)
}
function allErrorsHere(action, params) {
if (action === 'newFlowDone') {
// rejected from newFlow with params
}
}
function allNotifiesHere(action, params) {
// same as allErrorsHere
}
Installation
npm i better-flow --save
API
Setup
new Flow(options)
The available options are:
result
- set the property name in the response for resolveerror
- set the property name in the response for rejectnotify
- set the property name in the response for notify
new Flow({
result: 'done'
});
function doSomeThing(params) {
if (params.done) {
// resolved
}
}
License (MIT)
Copyright (c) Petar Telbiyski telbiyski@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.