saito200S
Convex Community2y ago
7 replies
saito200

How to use the result of a scheduled action?

My mutation schedules an action using await ctx.scheduler.runAfter(0, ...)

The action returns a value that then is used by the mutation to write to the DB, or that's what I planned

Pseudocode:
myMutation = mutation({
  handler: async (ctx) => {
    const res = await ctx.scheduler.runAfter(0, ..run action..)
    // write res to DB
  }
})


But this is not how it works, right? Because the return value of a scheduled action is a promise that resolves to a Id<"_scheduled_functions">

So it looks like I have to schedule the action, and from the action itself run an internal mutation that writes the output of the action to the DB, i.e.:

1. Run public mutation
2. Schedule action
3. Action gets some external data
4. Action runs internal mutation to write to DB

Is that second way the "correct way"?
Was this page helpful?