Child workflows send a signal to the parent

Hi,
I need to create multiple child workflows in parent workflow and the parent needs to wait for all of the child workflows to be finished.
Can I send a signal from the child to the parent? Is there any code example in typescript?
What’s the best patern to wait for all of the child workflows to be finished?

Thanks.

1 Like

You can start them async and wait for their completion, similar to this sample ( await Promise.all )

Hi,
In my use case I can’t wait for all of them, I need to be able to send data back (quering the parent) if any child has finished.
Is there any way to do this?
Can I send a signal from the child to notify the parent with the result?

Thanks.

Can I send a signal from the child to notify the parent with the result?

Yes, you can get the parent workflow id and runid inside child via:

workflowInfo().parent?.workflowId;
workflowInfo().parent?.runId;

Can you explain your use case please? Would help with knowing if sending signals from child workflows is best option for it or not.

I can’t wait for all of them

Why not?

Hi,
The child workflows would process some data like, for example, send some external request but that request can take time, like 5-10 minutes. An example would be: the parent creates four children, child one and two process the data stright away but the rest doesn’t(send some request or wait for some external processing that can take 5 - 10 minutes). I would like to have access to that partial results and that’s why I’d like to notify the parent when a child has finished, so I could update some parent state and query it from outside.

Thanks.

This updates the results state whenever a child finishes, but still waits for all of them to complete before returning:

async function myWorkflow(): Promise<string> {
  let results = [];
  await Promise.all([
    executeChild().then(result => results.push[result]), 
    executeChild().then(result => results.push[result])
  ])
  return 'done'
}

Should be

    executeChild().then(result => results.push(result)), 

The main issue here is that I still need to wait for all of them to get the result.
Could I write some loop or interval checker without getting a non-deterministic error?

The Promise.all resolves when you have all the results

what I need to do is the ability to query the parent while is getting results from children, that’s why promise all won’t work in this case. Is there any pattern that I can use?

The below query will first return [], then [‘firstresult’], then [‘firstresult’, ‘secondresult’].

import { executeChild, defineQuery, setHandler } from '@temporalio/workflow';

const getResults = defineQuery<string[]>('getResults');

async function myWorkflow(): Promise<string> {
  let results: string[] = [];

  setHandler(getResults, () => results);

  await Promise.all([
    executeChild().then(result => results.push(result)), 
    executeChild().then(result => results.push(result))
  ]);
  return 'done';
}

Thanks, this was what I was looking for. I’m gonna try to write some complex workflows to see how it works.