run<R> method

Future<R> run<R>(
  1. FutureOr<R> task()
)

run task on a worker and give its result. An error of the task is thrown here, with the stack trace of the worker.

Inside a worker, task runs in that same isolate. A worker is already off the request isolate, and a nested pool would never close.

Implementation

Future<R> run<R>(FutureOr<R> Function() task) {
  if (_ComputeWorker.inWorker) return Future<R>.sync(task);
  _Call<R> call = _Call<R>();
  _Job job = _Job(task, call.port.sendPort);
  SendPort? owner = _owner;
  if (owner == null) {
    _inbox ??= _openInbox();
    _add(job);
    return call.done.future;
  }
  try {
    owner.send(job);
  } catch (error, stackTrace) {
    call.fail(_notSendable(error), stackTrace);
  }
  // ponytail: a task sent after the owner closed (during shutdown) is
  // lost and never ends. Add a timeout if this shows up in practice.
  return call.done.future;
}