0

I just updated to the newest version of react-async (8.0.0) and the new promise prop is giving me some trouble.

I'm trying to use the promise prop after my useAsync hook, as described in the release notes, but I'm getting a runtime error "TypeError: Cannot read property 'then' of undefined".

const { data: result, run, setData, promise } = useAsync({
    promiseFn: getPhoto,
    deferFn: postPhoto,
    userId: userId
  });
  promise.then(res => setData(res));
Kat Tow
  • 3
  • 2

1 Answers1

0

This is because promise is only defined when the promiseFn is invoked. This means it will be undefined at least until the second render. Because this behavior is inconsistent with the TS typings and unintuitive, it will be fixed in the next release.

Gert Hengeveld
  • 1,849
  • 15
  • 12