Skip to content

Commit 801564b

Browse files
committed
Rename 'pending' to 'waiting' and 'loading' to 'pending'.
1 parent ab72f86 commit 801564b

File tree

8 files changed

+195
-108
lines changed

8 files changed

+195
-108
lines changed

README.md

Lines changed: 94 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,14 @@ is set to `"application/json"`.
382382
- `data` Last resolved promise value, maintained when new error arrives.
383383
- `error` Rejected promise reason, cleared when new data arrives.
384384
- `initialValue` The data or error that was provided through the `initialValue` prop.
385-
- `isPending` true when no promise has ever started, or one started but was cancelled.
386-
- `isLoading` true when a promise is currently awaiting settlement.
387-
- `isResolved` true when the last promise was fulfilled with a value.
388-
- `isRejected` true when the last promise was rejected with a reason.
389385
- `startedAt` When the current/last promise was started.
390386
- `finishedAt` When the last promise was resolved or rejected.
387+
- `status` One of: `waiting`, `pending`, `fulfilled`, `rejected`.
388+
- `isWaiting` true when no promise has ever started, or one started but was cancelled.
389+
- `isPending` true when a promise is currently awaiting settlement.
390+
- `isFulfilled` true when the last promise was fulfilled with a value.
391+
- `isRejected` true when the last promise was rejected with a reason.
392+
- `isSettled` true when the last promise was fulfilled or rejected (not waiting or pending).
391393
- `counter` The number of times a promise was started.
392394
- `cancel` Cancel any pending promise.
393395
- `run` Invokes the `deferFn`.
@@ -413,12 +415,6 @@ Rejected promise reason, cleared when new data arrives.
413415
414416
The data or error that was originally provided through the `initialValue` prop.
415417

416-
#### `isLoading`
417-
418-
> `boolean`
419-
420-
`true` while a promise is pending, `false` otherwise.
421-
422418
#### `startedAt`
423419

424420
> `Date`
@@ -431,6 +427,47 @@ Tracks when the current/last promise was started.
431427
432428
Tracks when the last promise was resolved or rejected.
433429

430+
#### `status`
431+
432+
> `string`
433+
434+
One of: `waiting`, `pending`, `fulfilled`, `rejected`.
435+
These are available for import as `statusTypes`.
436+
437+
#### `isWaiting`
438+
439+
> `boolean`
440+
441+
`true` while no promise has started yet, or one was started but cancelled.
442+
443+
#### `isPending`
444+
445+
> `boolean`
446+
447+
`true` while a promise is pending (loading), `false` otherwise.
448+
449+
Alias: `isLoading`
450+
451+
#### `isFulfilled`
452+
453+
> `boolean`
454+
455+
`true` when the last promise was fulfilled (resolved) with a value.
456+
457+
Alias: `isResolved`
458+
459+
#### `isRejected`
460+
461+
> `boolean`
462+
463+
`true` when the last promise was rejected with an error.
464+
465+
#### `isSettled`
466+
467+
> `boolean`
468+
469+
`true` when the last promise was either fulfilled or rejected (i.e. not waiting or pending)
470+
434471
#### `counter`
435472

436473
> `number`
@@ -474,10 +511,45 @@ invoked after the state update is completed. Returns the error to enable chainin
474511
React Async provides several helper components that make your JSX more declarative and less cluttered.
475512
They don't have to be direct children of `<Async>` and you can use the same component several times.
476513

477-
### `<Async.Loading>`
514+
### `<Async.Waiting>`
515+
516+
Renders only while the deferred promise is still waiting to be run, or you have not provided any promise.
517+
518+
#### Props
519+
520+
- `persist` `boolean` Show until we have data, even while loading or when an error occurred. By default it hides as soon as the promise starts loading.
521+
- `children` `function(state: object): Node | Node` Render function or React Node.
522+
523+
#### Examples
524+
525+
```js
526+
<Async deferFn={deferFn}>
527+
<Async.Waiting>
528+
<p>This text is only rendered while `run` has not yet been invoked on `deferFn`.</p>
529+
</Async.Waiting>
530+
</Async>
531+
```
532+
533+
```js
534+
<Async.Waiting persist>
535+
{({ error, isLoading, run }) => (
536+
<div>
537+
<p>This text is only rendered while the promise has not resolved yet.</p>
538+
<button onClick={run} disabled={!isLoading}>
539+
Run
540+
</button>
541+
{error && <p>{error.message}</p>}
542+
</div>
543+
)}
544+
</Async.Waiting>
545+
```
546+
547+
### `<Async.Pending>`
478548

479549
This component renders only while the promise is loading (unsettled).
480550

551+
Alias: `<Async.Loading>`
552+
481553
#### Props
482554

483555
- `initial` `boolean` Show only on initial load (when `data` is `undefined`).
@@ -486,19 +558,21 @@ This component renders only while the promise is loading (unsettled).
486558
#### Examples
487559

488560
```js
489-
<Async.Loading initial>
561+
<Async.Pending initial>
490562
<p>This text is only rendered while performing the initial load.</p>
491-
</Async.Loading>
563+
</Async.Pending>
492564
```
493565

494566
```js
495-
<Async.Loading>{({ startedAt }) => `Loading since ${startedAt.toISOString()}`}</Async.Loading>
567+
<Async.Pending>{({ startedAt }) => `Loading since ${startedAt.toISOString()}`}</Async.Pending>
496568
```
497569

498-
### `<Async.Resolved>`
570+
### `<Async.Fulfilled>`
499571

500572
This component renders only when the promise is fulfilled with data (`data !== undefined`).
501573

574+
Alias: `<Async.Resolved>`
575+
502576
#### Props
503577

504578
- `persist` `boolean` Show old data while loading new data. By default it hides as soon as a new promise starts.
@@ -507,11 +581,11 @@ This component renders only when the promise is fulfilled with data (`data !== u
507581
#### Examples
508582

509583
```js
510-
<Async.Resolved persist>{data => <pre>{JSON.stringify(data)}</pre>}</Async.Resolved>
584+
<Async.Fulfilled persist>{data => <pre>{JSON.stringify(data)}</pre>}</Async.Fulfilled>
511585
```
512586

513587
```js
514-
<Async.Resolved>{({ finishedAt }) => `Last updated ${startedAt.toISOString()}`}</Async.Resolved>
588+
<Async.Fulfilled>{({ finishedAt }) => `Last updated ${startedAt.toISOString()}`}</Async.Fulfilled>
515589
```
516590

517591
### `<Async.Rejected>`
@@ -533,39 +607,15 @@ This component renders only when the promise is rejected.
533607
<Async.Rejected>{error => `Unexpected error: ${error.message}`}</Async.Rejected>
534608
```
535609

536-
### `<Async.Pending>`
610+
### `<Async.Settled>`
537611

538-
Renders only while the deferred promise is still pending (not yet run), or you have not provided any promise.
612+
This component renders only when the promise is fulfilled or rejected.
539613

540614
#### Props
541615

542-
- `persist` `boolean` Show until we have data, even while loading or when an error occurred. By default it hides as soon as the promise starts loading.
616+
- `persist` `boolean` Show old data or error while loading new data. By default it hides as soon as a new promise starts.
543617
- `children` `function(state: object): Node | Node` Render function or React Node.
544618

545-
#### Examples
546-
547-
```js
548-
<Async deferFn={deferFn}>
549-
<Async.Pending>
550-
<p>This text is only rendered while `run` has not yet been invoked on `deferFn`.</p>
551-
</Async.Pending>
552-
</Async>
553-
```
554-
555-
```js
556-
<Async.Pending persist>
557-
{({ error, isLoading, run }) => (
558-
<div>
559-
<p>This text is only rendered while the promise has not resolved yet.</p>
560-
<button onClick={run} disabled={!isLoading}>
561-
Run
562-
</button>
563-
{error && <p>{error.message}</p>}
564-
</div>
565-
)}
566-
</Async.Pending>
567-
```
568-
569619
## Usage examples
570620

571621
Here's several examples to give you an idea of what's possible with React Async. For fully working examples, please

src/Async.js

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -179,47 +179,47 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
179179
}
180180

181181
/**
182-
* Renders only when deferred promise is pending (not yet run).
182+
* Renders only when deferred promise is waiting (promise has not yet run).
183183
*
184184
* @prop {Function|Node} children Function (passing state) or React node
185-
* @prop {boolean} persist Show until we have data, even while loading or when an error occurred
185+
* @prop {boolean} persist Show until we have data, even while pending (loading) or when an error occurred
186186
*/
187-
const Pending = ({ children, persist }) => (
187+
const Waiting = ({ children, persist }) => (
188188
<Consumer>
189189
{state => {
190190
if (state.data !== undefined) return null
191-
if (!persist && state.isLoading) return null
191+
if (!persist && state.isPending) return null
192192
if (!persist && state.error !== undefined) return null
193193
return isFunction(children) ? children(state) : children || null
194194
}}
195195
</Consumer>
196196
)
197197

198198
if (PropTypes) {
199-
Pending.propTypes = {
199+
Waiting.propTypes = {
200200
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
201201
persist: PropTypes.bool,
202202
}
203203
}
204204

205205
/**
206-
* Renders only while loading.
206+
* Renders only while pending (promise is loading).
207207
*
208208
* @prop {Function|Node} children Function (passing state) or React node
209209
* @prop {boolean} initial Show only on initial load (data is undefined)
210210
*/
211-
const Loading = ({ children, initial }) => (
211+
const Pending = ({ children, initial }) => (
212212
<Consumer>
213213
{state => {
214-
if (!state.isLoading) return null
214+
if (!state.isPending) return null
215215
if (initial && state.data !== undefined) return null
216216
return isFunction(children) ? children(state) : children || null
217217
}}
218218
</Consumer>
219219
)
220220

221221
if (PropTypes) {
222-
Loading.propTypes = {
222+
Pending.propTypes = {
223223
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
224224
initial: PropTypes.bool,
225225
}
@@ -229,21 +229,21 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
229229
* Renders only when promise is resolved.
230230
*
231231
* @prop {Function|Node} children Function (passing data and state) or React node
232-
* @prop {boolean} persist Show old data while loading
232+
* @prop {boolean} persist Show old data while pending (promise is loading)
233233
*/
234-
const Resolved = ({ children, persist }) => (
234+
const Fulfilled = ({ children, persist }) => (
235235
<Consumer>
236236
{state => {
237237
if (state.data === undefined) return null
238-
if (!persist && state.isLoading) return null
238+
if (!persist && state.isPending) return null
239239
if (!persist && state.error !== undefined) return null
240240
return isFunction(children) ? children(state.data, state) : children || null
241241
}}
242242
</Consumer>
243243
)
244244

245245
if (PropTypes) {
246-
Resolved.propTypes = {
246+
Fulfilled.propTypes = {
247247
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
248248
persist: PropTypes.bool,
249249
}
@@ -253,13 +253,13 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
253253
* Renders only when promise is rejected.
254254
*
255255
* @prop {Function|Node} children Function (passing error and state) or React node
256-
* @prop {boolean} persist Show old error while loading
256+
* @prop {boolean} persist Show old error while pending (promise is loading)
257257
*/
258258
const Rejected = ({ children, persist }) => (
259259
<Consumer>
260260
{state => {
261261
if (state.error === undefined) return null
262-
if (state.isLoading && !persist) return null
262+
if (state.isPending && !persist) return null
263263
return isFunction(children) ? children(state.error, state) : children || null
264264
}}
265265
</Consumer>
@@ -272,16 +272,43 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
272272
}
273273
}
274274

275-
Async.Pending = Pending
276-
Async.Loading = Loading
277-
Async.Resolved = Resolved
278-
Async.Rejected = Rejected
275+
/**
276+
* Renders only when promise is fulfilled or rejected.
277+
*
278+
* @prop {Function|Node} children Function (passing state) or React node
279+
* @prop {boolean} persist Show old data or error while pending (promise is loading)
280+
*/
281+
const Settled = ({ children, persist }) => (
282+
<Consumer>
283+
{state => {
284+
if (state.isWaiting) return null
285+
if (state.isPending && !persist) return null
286+
return isFunction(children) ? children(state) : children || null
287+
}}
288+
</Consumer>
289+
)
290+
291+
if (PropTypes) {
292+
Settled.propTypes = {
293+
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
294+
persist: PropTypes.bool,
295+
}
296+
}
297+
298+
Waiting.displayName = `${displayName}.Waiting`
299+
Pending.displayName = `${displayName}.Pending`
300+
Fulfilled.displayName = `${displayName}.Fulfilled`
301+
Rejected.displayName = `${displayName}.Rejected`
302+
Settled.displayName = `${displayName}.Settled`
279303

280304
Async.displayName = displayName
281-
Async.Pending.displayName = `${displayName}.Pending`
282-
Async.Loading.displayName = `${displayName}.Loading`
283-
Async.Resolved.displayName = `${displayName}.Resolved`
284-
Async.Rejected.displayName = `${displayName}.Rejected`
305+
Async.Waiting = Waiting
306+
Async.Pending = Pending
307+
Async.Loading = Pending // alias
308+
Async.Fulfilled = Fulfilled
309+
Async.Resolved = Fulfilled // alias
310+
Async.Rejected = Rejected
311+
Async.Settled = Settled
285312

286313
return Async
287314
}

0 commit comments

Comments
 (0)