You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/actors.mdx
+69Lines changed: 69 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -408,6 +408,75 @@ Callback actors are a bit different from other actors in that they do not do the
408
408
- Do not emit values when used with `.subscribe()`
409
409
- Can not be stopped with `.stop()`
410
410
411
+
Callback functions cannot be `async` functions. But it is possible to execute a Promise within a callback function. Promise rejections will not be caught by `onError`, so you may choose to use `sendBack` to report errors caught from the Promise to the parent actor.
To use `async`/`await`, you may find it more expressive to wrap your code in an [asynchronous immediately invoked function expression (IIFE)](https://developer.mozilla.org/en-US/docs/Glossary/IIFE#execute_an_async_function).
444
+
445
+
```ts
446
+
const machine =createMachine({
447
+
initial: 'running',
448
+
states: {
449
+
running: {
450
+
invoke: {
451
+
src: fromCallback(({ sendBack }) => {
452
+
// highlight-start
453
+
(async () => {
454
+
try {
455
+
const data =awaitsomePromise();
456
+
sendBack({ type: 'done', data });
457
+
} catch (error) {
458
+
sendBack({ type: 'error', data: error });
459
+
}
460
+
})();
461
+
// highlight-end
462
+
463
+
return () => {
464
+
/* cleanup function */
465
+
};
466
+
})
467
+
},
468
+
// highlight-start
469
+
on: {
470
+
error: {
471
+
actions: ({ event }) =>console.error(event.data)
472
+
}
473
+
}
474
+
// highlight-end
475
+
}
476
+
}
477
+
});
478
+
```
479
+
411
480
## Higher-level actor logic
412
481
413
482
Higher-level actor logic enhances existing actor logic with additional functionality. For example, you can create actor logic that logs or persists actor state:
0 commit comments