Spawning Promises. Error handling issue. #1779
-
I am aware the Docs recommend to "invoke" rather than "spawn" Promises, however if I "invoke" promises then, as far as I understand it, I cannot respond to their 'resolve'/'reject' if I exit the state they were "invoked" in. So I am spawning them as actors, which allows me to still handle them if they resolve after I have changed state. However I cannot figure out how to handle spawned promises that "reject". What is the equivalent to "onError" for actors? I've tried "error.invoke." (which was just a guess) but that didn't work. All I can find via a google search is people with issues handling promise rejections for INVOKED promises, not SPAWNED promises. Any help would be appreciated. Thank you. I'm very excited about this library. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Can you share a code reproduction of how you're currently spawning promises? |
Beta Was this translation helpful? Give feedback.
-
For sure. Here is a basic example with just one promise actor: https://codesandbox.io/s/xstatespawnedpromises-8ipt2?file=/src/index.ts |
Beta Was this translation helpful? Give feedback.
-
To handle errors for any spawned/invoked actor, you can use the import { error } from 'xstate/lib/actions';
// ...
[error("firstPromise")]: {
actions: (context, event) => {
console.log("promise error data:", event.data);
}
}, This is likely to be improved in v5. |
Beta Was this translation helpful? Give feedback.
To handle errors for any spawned/invoked actor, you can use the
error()
function to create an"error.platform.[name]"
string:This is likely to be improved in v5.