1.0.2 • Published 6 years ago
forib v1.0.2
Actions handler
Why?
fetch('url')
.then(response => response.json())
.then(response => {
fetch(url, options: {
header: {
'Some-Option': response.data...
}
})
.then(response2 => response2.json())
.then(response2 => {
// Again and again, and again fetch...
})
})
How to use?
You should use only the one function - forib
let state = 0
const firstAction = () => state = 1
const secondAction = () => state = 2
forib({
actions: [
firstAction, secondAction
]
})
console.log(state) // 2
But what if i wanna set some data to any action?
Just remember, you can set args
, globalArgs
and callbacks
and they will be parse by index of action!
let result = []
const firstAction = zero => result.push(zero)
const secondAction = one => result.push(one)
forib({
actions: [firstAction, secondAction],
args: [
[0],
[1]
]
})
console.log(result) // [0, 1]
Okay, it's cool! But i wanna set one argument for all actions. I need set it for each action?
Of course nope! You have the globalArgs
param for this!
let result = []
const firstAction = (zero, one, str) => result = [zero, one, str]
const secondAction = (zero, str) => console.log(zero, str); // 0, second action
forib({
actions: [firstAction, secondAction],
args: [
[1, 'first action'],
['second action']
],
globalArgs: [0]
})
console.log(result) // [0, 1, 'first action']
Also, you can set callbacks for any action! (When action will be finished, callbacks will calls)
const firstAction = () => console.log('first')
const secondAction = () => console.log('second')
const afterFirst = () => console.log('First action is finished!')
const afterSecond = () => console.log('Second action is finished!')
forib({
actions: [firstAction, secondAction],
callbacks: [
[afterFirst],
[afterSecond]
]
})
// First
// First action is finished
// Second
// Second action is finished