diff --git a/src/index.ts b/src/index.ts index ce26228..418243a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,19 +1,35 @@ -import {DependencyList, useEffect, useState} from 'react' +import { DependencyList, useRef, useEffect, useState, useReducer, useMemo } from 'react' + +export function useAsyncMemo(factory: () => Promise, deps?: DependencyList, initial?: T) { + let [val, setVal] = useState(initial) -export function useAsyncMemo(factory: () => Promise | undefined | null, deps: DependencyList, initial: T = undefined): T { - const [val, setVal] = useState(initial) useEffect(() => { - let cancel = false - const promise = factory() - if (promise === undefined || promise === null) return - promise.then((val) => { - if (!cancel) { - setVal(val) - } - }) + let pending = true + factory().then(res => pending && setVal(res)) + return () => { - cancel = true + pending = false } }, deps) + return val } + +export function useAsync(factory: () => Promise, deps?: DependencyList, initial?: T) { + let forceUpdate = useState()[1] + let val = useRef(initial) + + useMemo(() => val.current = initial, deps) + useEffect(() => { + let pending = true + factory().then(res => { + if(pending) + forceUpdate(val.current = res) + }) + return () => { + pending = false + } + }, deps) + + return val.current +}