@nutgaard/use-async
v2.3.1
Published
[](https://github.com/prettier/prettier) [](https://travis-ci.org/nutgaard/use-async) [.
import React from 'react';
import useAsync, { isPending, hasError } from '@nutgaard/use-async';
const source = React.useCallback((isRerun) => Promise.resolve("your data here"), []);
const result = useAsync(source);
if (isPending(result)) {
return <Spinner />;
} else if (hasError(result)) {
return <Error />
}
return <pre>{result.data}</pre>useAsync API
| Argument | Type | Optional | Default |
| ------------- | ------------- | ------------- | ------------- |
| source | (isRerun) => Promise<DATA> | No | - |
| lazy | boolean | Yes | false |
| dependencyList | Array<any> | Yes | undefiend |
In cases where dependencyList is defined it is passed on to useEffect instead of source.
This allows a greater control of when the effect should run in cases where the source does
not necessarily change.
Types
Full documentation of types can be seen here, or in the 80-ish lines of code.
Credits
Made using the awesome typescript library starter
