Execution Context
When deferring a task using flow.defer(), it’s often useful to carry over contextual information—like metadata or custom parameters—that can be accessed later during continuation.
This feature introduces a way to persist that context, so it becomes available inside the ContinuationHandler. You can now attach key-value pairs at the time of deferral and access them when the continuation handler is executed.
As of version 6.1.0, all chained deferred tasks share the same execution context.
Saving context
The following example demonstrates how to use the Flow object to add context data within a TaskExecutor implementation:
@Executor("example-task")
export class ExampleExecutor implements TaskExecutor {
async processTask(command: any, metadata: TaskMetadata, flow: Flow): Promise<ExampleResponse> {
const params = {
key: 'value1'
};
const context = await flow.context();
context.add('nested-task-params-key', params);
return flow
.defer()
.withTask('nested-task', params)
.continueOn("myContinuationHandler")
}
}
Retrieving context
The example below shows how to access the previously saved context data from the Flow object within your ContinuationHandler implementation:
@Continuation("myContinuationHandler")
export class MyContinuationHandler extends ContinuationHandler {
async onSuccess(command: TaskCommand, taskResult: TaskResult, metadata: TaskMetadata, flow: Flow) {
const context = await flow.context();
const paramsNestedTask = context.get('nested-task-params-key');
return {
...taskResult,
result: {
url: taskResult.result.url,
key: paramsNestedTask.key, // 'value1'
},
};
}
}
6 version breaking change
flow.context() is now asynchronous and must be awaited.