Sequential Child Workflows


The ability to run subworkflows during the execution of a main workflow opens up multi-step, sequential processes using Zenaton. This can be accomplished by sending an event from a running workflow to launch a subworkflwo (or 'child workflow').

For example, if a parent workflow's name is ParentWorkflow and the child workflow's name is ChildWorkflow,

module.exports.handle = function*() {
  ...
  const output = yield* this.runWorkflow("ChildWorkflow", ...childInput);
  // continue
  ...
};

module.exports.runWorkflow = function*(name, ...input) {
  // dispatch sub-workflow with parent's info
  this.run.workflow(name, this.context.id, ...input);
  // wait for completion event that will be sent by child workflow 
  const [_, output] = yield this.wait.event(name + '_' + this.context.id).forever();

  return output;
};

Update ChildWorkflow to receive parent Id as the first parameter and send a completion event to parent at the end of the child implementation:

module.exports.handle2 = function*(...input) {
  // Workflow implementation
  ...
  return output;
};

// updated signature to receive parent id and send completion event
module.exports.handle = function*(parentId, ...input) {
  const output = yield* this.handle2(...input);
  // When completed, send an event to parent workflow with output
  this.select.workflow().withId(parentId).send(this.constructor.name + '_' + parentId, output);
};


WARNING

Currently, you can NOT use yield keyword before this.run.workflow() to indicate that a child workflow should complete before continuing the parent workflow processing.