Uneventful
    Preparing search index...
    • Wrap an argument-taking function so it will run in (and returns) a new Task when called.

      This lets you avoid the common pattern of needing to write your functions or methods like this:

      function outer(arg1, arg2) {
      return start(function*() {
      // ...
      })
      }

      and instead write them like this:

      const outer = task(function *(arg1, arg2) {
      // ...
      });

      or this:

      class Something {
      ⁣⁣@task // auto-detects TC39 or legacy decorators
      *someMethod(arg1): Coroutine<SomeResultType> {
      // ...
      }
      }

      Important: if the wrapped function or method has overloads, the resulting function type will be based on the last overload, because TypeScript (at least as of 5.x) is still not very good at dealing with higher order generics, especially if overloads are involved.

      Also note that TypeScript doesn't allow decorators to change the calling signature or return type of a method, so even though the above method will return a Task, TypeScript will only see it as a Coroutine.

      This is fine if all you're going to do is yield * it to wait for the result, but if you need to use any task-specific methods on it, you'll have to pass it through start to have TypeScript treat it as an actual Task. (Luckily, start() has a fast path to return the original task if it's passed a task, so you won't actually create a new task by doing this.)

      Type Parameters

      • T
      • A extends any[]
      • C

      Parameters

      Returns (this: C, ...args: A) => Task<T>

      A wrapped version of the function that passes through its arguments to the original function, while running it in a new task. (The wrapper also returns the task.)