async-mobx
Getting async data for Mobx 3+.
Installation
npm
npm i async-mobx
yarn
yarn add async-mobx
Using
Async
is a Promise
like constructor
const promise = { }
then, catch, finally
then
, catch
and finally
always return instance of Promise
const test = instanceof Promise// test === true
Use then
, catch
and finally
like for Promise
const promise = promise
We have one specific think for mobx, if you return a function to resolve
or reject
then the function will be called when you need to get the result
async { let test = true const promise = // test still equals true await promise // test is false}
You may override the result at function to fix it
async { {} const promise = const result = await promise return result === test // true}
loading
You may check status of Async
with loading
, it's true
when data is loading
async { const promise = // promise.loading === true await promise // promise.loading === false}
loaded
You may check status of Async
with loaded
, it's true
when data was loaded at least one time
async { const promise = // promise.loaded === false await promise // promise.loaded === true}
value
You may get result without await
synchronously with value
const promise = // promise.value === 1
But value
returns result at the moment
async { const promise = // promise.value === undefined await promise // promise.value === 1}
error
You may handle error without await
synchronously with error
like value
with resolve
const promise = // promise.error === 1
default
You may provide default value
for Async
async { const promise = default: 1 // promise.value === 1 await promise // promise.value === 2}
response
response
is the same value
but without default value
async { const promise = default: 1 // promise.value === 1 // promise.response === undefined await promise // promise.value === 2 // promise.response === 2}
update
Unlike Promise
, you may reuse Async
with update
method
let i = 0const promise = // i === 1promise// i === 2
reset
The method just sets default value as current and clears error
const promise = default: 1promise // promise.value = 2promise // promise.value = 1
resolve
You may use resolve
to say async that loading is finished successfully
const promise = promise// promise.value === 1
reject
You may use reject
to say async that loading is finished with error
const promise = promise// promise.error === 1
on, once, off
You may add a listener to react on events.
Use resolve
, reject
or update
as a type of event.
const promise = let test = falsepromise// test === falsepromise// test === truepromise// test === false
You may add a listener which reacts only once with once
const promise = let test = falsepromise// test === falsepromise// test === truepromise// test === true
You may turn off a listener
const promise = let test = falseconst listener = test = promisevaluepromise// test === falsepromise// test === truepromisepromise// test === true
Issues
If you find a bug, please file an issue on GitHub