use-deferred
React hook to handle the deferred promise.
Install
npm i use-deferred
Example
import useDeferred from 'use-deferred' { const execute resolve reject isPending = return <div> <Page = /> isPending && <Popup = = /> </div>}
{ { try const popupResult = await /* ... */ catch err if errisClosed return /* ... */ } /* ... */}
{ { } { } /* ... */}
API
useDeferred(handlers?) : defer
Returns an object to handle the deferred promise.
handlers
Handler called when deferred state changes.
onExecute(...args)
onComplete()
onResolve(value)
onReject(reason?)
Example
const execute = ; // => 'HELLO WORLD'
State
Properties for the current state.
isBefore
isPending
isResolved
isRejected
isComplete
state
Example #1
{ const execute isBefore isPending = console console // => isBefore: true // => isPending: false // => isBefore: false // => isPending: true /* ... */}
Example #2
{ const execute state = ifstate === BEFORE console ifstate === PENDING console // => current state: BEFORE // => current state: PENDING /* ... */}
defer.execute(...args)
Execute a deferred promise. If there is an existing deferred promise that is not completed, return it.
defer.forceExecute(...args)
Reject an existing deferred promise and execute a new deferred promise.
Example
{ const execute forceExecute = // First run. { try await catcherr console console console } // Second. { await // => 1. true // => 2. true // => 3. 'Canceled by forced execution.' } /* ... */}
defer.resolve(value)
Resolve the current pending promise.
defer.reject(reason?)
Reject the current pending promise.
License
MIT