Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make sure the promise render prop is always a Promise.
  • Loading branch information
ghengeveld committed Sep 28, 2019
commit 4bd65be88b9962bc8cc03e1dc1de33e33239524f
10 changes: 8 additions & 2 deletions packages/react-async/src/Async.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import React from "react"
import globalScope from "./globalScope"
import { IfInitial, IfPending, IfFulfilled, IfRejected, IfSettled } from "./helpers"
import propTypes from "./propTypes"
import { actionTypes, init, dispatchMiddleware, reducer as asyncReducer } from "./reducer"
import {
neverSettle,
actionTypes,
init,
dispatchMiddleware,
reducer as asyncReducer,
} from "./reducer"

/**
* createInstance allows you to create instances of Async that are bound to a specific promise.
Expand Down Expand Up @@ -32,7 +38,7 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
this.mounted = false
this.counter = 0
this.args = []
this.promise = undefined
this.promise = neverSettle
this.abortController = { abort: () => {} }
this.state = {
...init({ initialValue, promise, promiseFn }),
Expand Down
4 changes: 3 additions & 1 deletion packages/react-async/src/reducer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { getInitialStatus, getIdleStatus, getStatusProps, statusTypes } from "./status"

export const neverSettle = new Promise(() => {})

export const actionTypes = {
start: "start",
cancel: "cancel",
Expand All @@ -16,7 +18,7 @@ export const init = ({ initialValue, promise, promiseFn }) => ({
finishedAt: initialValue ? new Date() : undefined,
...getStatusProps(getInitialStatus(initialValue, promise || promiseFn)),
counter: 0,
promise: undefined,
promise: neverSettle,
})

export const reducer = (state, { type, payload, meta }) => {
Expand Down
10 changes: 8 additions & 2 deletions packages/react-async/src/useAsync.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { useCallback, useDebugValue, useEffect, useMemo, useRef, useReducer } from "react"

import globalScope from "./globalScope"
import { actionTypes, init, dispatchMiddleware, reducer as asyncReducer } from "./reducer"
import {
neverSettle,
actionTypes,
init,
dispatchMiddleware,
reducer as asyncReducer,
} from "./reducer"

const noop = () => {}

Expand All @@ -12,7 +18,7 @@ const useAsync = (arg1, arg2) => {
const isMounted = useRef(true)
const lastArgs = useRef(undefined)
const lastOptions = useRef(undefined)
const lastPromise = useRef(undefined)
const lastPromise = useRef(neverSettle)
const abortController = useRef({ abort: noop })

const { devToolsDispatcher } = globalScope.__REACT_ASYNC__
Expand Down