Easily observe asynchronous processes
The purpose of this hook is to facilitate the consumption of promises.
where:
callback
: asynchronous function.
args
: arguments that callback requires.
autorun
: by default true, it automatically executes the promise, if it is false the execution of the promise is suspended.
promise
: return object, at the type level it defines the following properties:
pending
: boolean, defines if the promise is executed but pending resolution.
fulfilled
: boolean, defines if the promise has been fulfilled
result
: result of the promise.
rejected
: boolean, defines if the promise has been rejected.
Note: When using Typescript Do not force the types for the second argument of usePromise, as usePromise will infer them from your callback.
It is preferable to use ternary or if to condition the reading of states, since this will help the definition of result
Note: At the type level, autocompletion is conditional on the state you are looking to observe.