-
I have these machines: const postMachine = createMachine({
id: "poster",
initial: "idle",
context: {
posts: [],
user: undefined
},
states: {
idle: {
on: {
FETCHED: {
target: "fetched",
actions: async (ctx, evt) => {
console.log(await ctx.user);
}
}
}
},
fetched: {
type: "final"
}
}
}); export const fetcherMachine = createMachine(
{
id: "user",
initial: "loading",
context: {
user: {}
},
states: {
loading: {
on: {
FETCH: "success"
}
},
success: {
entry: "fetchUser",
invoke: {
id: "poster",
src: postMachine,
data: {
user: (ctx) => ctx.user
},
onDone: (ctx, evt) => console.log("not reaching this point. why?")
},
exit: send("FETCHED", { to: "poster" }),
on: {
RESOLVE: {
target: "exit"
}
}
},
exit: {
type: "final"
}
}
},
{
actions: {
fetchUser: assign({
user: () => {
async function fetchUser() {
let user = await fetch(`https://api.github.com/users/ivorpad`);
user = await user.json();
return user.avatar_url;
}
return fetchUser();
}
})
}
}
); Relevant CodeSandbox: https://codesandbox.io/s/modern-http-33pn9 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
This is structured in a quite weird way - your Side note: you can't use |
Beta Was this translation helpful? Give feedback.
This is structured in a quite weird way - your
onDone
is specific to the invoked"poster"
, it gets canceled when exiting the containingsuccess
state. You can't receive events from canceled children services and even if you would be able to receive them you wouldn't be able to process it here because by the time you would receive such event you would be already in theexit
state which has no transition defined for this "done" event.Side note: you can't use
async
functions withinassign
. This won't work correctly.