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
fnforeffect - If
effectwas 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
fnon eacheffectcall, pass payload ofeffectas argument tofn - When
unwatchis 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
eventis triggered, callfnwith payload fromevent, then triggereffectwith 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
firstis triggered, pass payload fromfirsttofn - Trigger
secondwith 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
doneDatais an event, that triggered wheneffectis successfully resolved withresultfrom.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
failDatais an event, that triggered wheneffectis rejected witherrorfrom.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 (doneorfail)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
$storewill update whendoneorfailare triggered$storecontainstruevalue 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
$countwill be0if no calls ofeffectin pending state, its default state - On each call of
effectstate in$countstore will be increased - When effect resolves to any state(done or fail) state in
$countstore 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.