[XState] Invoke with callback
Invoke not only able to return a promise, but also a callback function with 'sendBack' & 'receive' callbacks
invoke: { // nested state with id reference id: "child", // return a callback function src: (context, event) => (sendBack, receive) => { setTimeout(() => { // send out a event sendBack({ type: "I_AM_DONE", }); }, 5000); // wait for receiving a event receive((event) => { console.log("event", event); if (event.type === "SNED_IT_ALREADY") { sendBack({ type: "I_AM_DONE", }); } }); },
Receive a event:
on: { RESOLVE: "resolved", // cancel the promise CANCEL: "idle", I_AM_DONE: "resolved", SNED_IT_ALREADY: { actions: send( { type: "SNED_IT_ALREADY", }, { to: "child", } ), }, },
--- Full code---
import { createMachine, assign, interpret, send } from "xstate"; const elBox = document.querySelector("#box"); const randomFetch = () => { return new Promise((res, rej) => { setTimeout(() => { if (Math.random() < 0.5) { rej("Fetch failed!"); } else { res("Fetch succeeded!"); } }, 2000); }); }; const machine = createMachine({ initial: "idle", states: { idle: { on: { FETCH: "pending", }, }, pending: { on: { RESOLVE: "resolved", // cancel the promise CANCEL: "idle", I_AM_DONE: "resolved", SNED_IT_ALREADY: { actions: send( { type: "SNED_IT_ALREADY", }, { to: "child", } ), }, }, invoke: { // Invoke your promise here. // The `src` should be a function that returns the source. /* Promise src: (context, event) => { return randomFetch(); }, */ id: "child", src: (context, event) => (sendBack, receive) => { setTimeout(() => { sendBack({ type: "I_AM_DONE", }); }, 5000); receive((event) => { console.log("event", event); if (event.type === "SNED_IT_ALREADY") { sendBack({ type: "I_AM_DONE", }); } }); }, onDone: { target: "resolved", }, onError: { target: "rejected", }, }, }, resolved: { // Add a transition to fetch again on: { FETCH: "pending", }, }, rejected: { // After 2s, retry again after: { 2000: { target: "pending", }, }, }, }, }); const service = interpret(machine); service.onTransition((state) => { elBox.dataset.state = state.toStrings().join(" "); console.log(state); }); service.start(); elBox.addEventListener("click", (event) => { service.send("FETCH"); }); const cancelBtn = document.querySelector("#cancel"); cancelBtn.addEventListener("click", (event) => { service.send("CANCEL"); }); const receiveBtn = document.querySelector("#receive"); receiveBtn.addEventListener("click", (event) => { service.send("SNED_IT_ALREADY"); });
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2019-08-13 [HTML5] Add Semantic Styling to the Current Page of a Navigation Item with aria-current
2018-08-13 [Algorithms] Insertion sort algorithm using TypeScript
2016-08-13 [RxJS] just, return
2016-08-13 [AngularJS] 'require' prop in Directive or Component