Effect
Effect is a container for async function.
It can be safely used in place of the original async function.
Arguments
params
(Params): parameters passed to effect
Returns
(Promise
)
Example
Effect Methods
use(handler)
Provides a function, which will be called when effect is triggered.
Formulae
- Set handler
fn
foreffect
- If
effect
was handler before, it will be replaced - Hint: current handler can be extracted with
effect.use.getCurrent()
note
You must provide a handler either through .use
method or handler
property in createEffect, otherwise effect will throw with "no handler used in %effect name%" error when effect will be called
Arguments
handler
(Function): Function, that receives the first argument passed to an effect call.
Returns
(Effect): The same effect
Example
watch(watcher)
Subscribe to effect calls.
Formulae
- Call
fn
on eacheffect
call, pass payload ofeffect
as argument tofn
- When
unwatch
is called, stop callingfn
Arguments
watcher
(Watcher): A function that receivespayload
.
Returns
Subscription: Unsubscribe function.
Example
prepend(fn)
Creates an event, upon trigger it sends transformed data into the source event. Works kind of like reverse .map
. In case of .prepend
data transforms before the original event occurs and in the case of .map
, data transforms after original event occurred.
Formulae
- When
event
is triggered, callfn
with payload fromevent
, then triggereffect
with result offn()
Arguments
fn
(Function): A function that receivespayload
, should be pure.
Returns
Event: New event.
map(fn)
Creates a new event, which will be called after the original effect is called, applying the result of a fn
as a payload. It is special function which allows you to decompose dataflow, extract or transform data.
Formulae
- When
first
is triggered, pass payload fromfirst
tofn
- Trigger
second
with the result of thefn()
call as payload
Arguments
fn
(Function): A function that receivespayload
, should be pure.
Returns
Event: New event.
Example
use.getCurrent()
Returns current handler of effect. Useful for testing.
Formulae
- Returns current handler for
effect
- If no handler was assigned to
effect
, default handler will be returned (that throws an error) - Hint: to set a new handler use
effect.use(handler)
Returns
(Function): Current handler, defined by handler
property or via use
call.
Example
Effect Properties
doneData
Event, which is triggered with result of the effect execution:
Formulae
doneData
is an event, that triggered wheneffect
is successfully resolved withresult
from.done
Important
Do not manually call this event. It is event that depends on effect.
since
effector 20.12.0
Event triggered when handler is resolved.
Example
failData
Event, which is triggered with error thrown by the effect
Formulae
failData
is an event, that triggered wheneffect
is rejected witherror
from.fail
Important
Do not manually call this event. It is event that depends on effect.
since
effector 20.12.0
Event triggered when handler is rejected or throws error.
Example
done
Event, which is triggered when handler is resolved.
Important
Do not manually call this event. It is event that depends on effect.
Properties
Event triggered with object of params
and result
:
params
(Params): An argument passed to the effect callresult
(Done): A result of the resolved handler
Example
fail
Event, which is triggered when handler is rejected or throws error.
Important
Do not manually call this event. It is event that depends on effect.
Properties
Event triggered with object of params
and error
:
params
(Params): An argument passed to effect callerror
(Fail): An error catched from the handler
Example
finally
since
effector 20.0.0
Event, which is triggered when handler is resolved, rejected or throws error.
Important
Do not manually call this event. It is event that depends on effect.
Properties
Event, which is triggered with object of status
, params
and error
or result
:
status
(string): A status of effect (done
orfail
)params
(Params): An argument passed to effect callerror
(Fail): An error catched from the handlerresult
(Done): A result of the resolved handler
Example
pending
Formulae
$store
will update whendone
orfail
are triggered$store
containstrue
value until the effect is resolved or rejected
Important
Do not modify $store
value! It is derived store and should be in predictable state.
Example
It's a shorthand for common use case
inFlight
Formulae
- Store
$count
will be0
if no calls ofeffect
in pending state, its default state - On each call of
effect
state in$count
store will be increased - When effect resolves to any state(done or fail) state in
$count
store will be decreased
Important
Do not modify $store
value! It is derived store and should be in predictable state.
since
effector 20.11.0
Store which show how many effect calls aren't settled yet. Useful for rate limiting.