then<R> method

  1. @override
Job<R> then<R>(
  1. FutureOr<R> onValue(
    1. JobContext ctx,
    2. T value
    ), {
  2. Object? key,
  3. String describe()?,
  4. JobObserver? observer,
})
override

Creates a job that runs onValue after this job succeeds and cleans up.

The callback receives its own JobContext and this job's value, is called asynchronously even when this job has already finished, and may return a value or a future. A failed source forwards its error and stack trace without calling onValue, and the continuation answers for the failure from then on. A job is continued once: a second call throws a StateError before anything is made, and so does a call on a job that shared has continued; give each branch a handle of shared. An engine that hands one job to several callers may refuse this call from the first one on.

The continuation is a root job of the core with its own optional observer, not a ChildJob, and awaiting it from its source's body or cleanup deadlocks. key and describe name it as they name any job, see A job's name; without describe its description is then.

final length = Job<String>((ctx) async => 'hello')
    .then<int>((ctx, text) => text.length);
print(await length.value); // 5

See Chains for cancelling a chain, a link that fails and what the callback returns.

Implementation

@override
Job<R> then<R>(
  FutureOr<R> Function(JobContext ctx, T value) onValue, {
  Object? key,
  String Function()? describe,
  JobObserver? observer,
}) {
  _continueBy(_Continuation.then);
  return _ThenJob<T, R>(
    this,
    onValue,
    key: key,
    describe: describe ?? () => 'then',
    observer: observer,
  );
}