Support JS promises
Opened this issue · 4 comments
It would be cool if you could pass a Promise
to createAsyncRenderer
instead of the status object. The resolved promise result should be passed to the completed successfully callback.
The renderer expects the consumer to manage the long running process and expects the consumer to either re-create the renderer when the process's status changes or change the status in the arguments reference that is passed to the renderer creator function. To pass the ongoing long running process to the renderer creator function pushes the responsibility of managing its status changes to the renderer.
I think this feature can be added to the renderer but I wonder if it makes sense given the current pattern.
That being said, a cool pattern may be to do something like this in a React + Redux environment:
function MyData(props: { id: number }) {
const dispatch = useAppDispatch()
const dataRenderer = createAsyncRenderer(dispatch(asyncThunks.loadData(id)))
return dataRenderer(data => (<div>{data}</div>))
}
Actually, that doesn't make sense because you should not dispatch a thunk like that in the render function (🤦♂️).
maybe something like this would make more sense
function MyData(props: { id: number }) {
const dispatch = useAppDispatch()
const loadMyData = useCallback(() => dispatch(asyncThunks.loadData(id)), [dispatch])
const dataRenderer = useCreateAsyncRenderer(loadMyData)
return dataRenderer(data => (<div>{data}</div>))
}