Class: Yieldable

Yieldable()

new Yieldable()

Yieldables are a primitive for building safe, cancelation-aware ways to instrument and introspect the runtime of a task. Many Yieldables are built-in to ember-concurrency today, such as timeout, animationFrame, and rawTimeout.

For example, if I wanted to implement a yieldable for requestIdleCallback, I could do the following:

import Component from '@glimmer/component';
import { task, Yieldable } from 'ember-concurrency';

class IdleCallbackYieldable extends Yieldable {
  onYield(state) {
    let callbackId = requestIdleCallback(() => state.next());

    return () => cancelIdleCallback(callbackId);
  }
}

const idleCallback = () => new IdleCallbackYieldable();

class MyComponent extends Component {
  @task *backgroundTask() {
    while (1) {
      yield idleCallback();

      const data = this.complicatedNumberCrunching();
      yield this.sendData(data);
    }
  }
}

In general, Yieldable instances should be reusable across calls, and thus care should be taken to ensure that teardown is provided and state not intended to be shared across calls stay inside onYield.

Yieldable also provides automatic Promise-casting.

Source:

Methods

catch() → {Promise}

Source:
Returns:
Type
Promise

finally() → {Promise}

Source:
Returns:
Type
Promise

onYield(state)

Defines what happens when the task encounters yield myYieldable and returns a disposer function that handles any cleanup.

The state parameter is provided by the runtime, and provides operations for interacting with the yielding task instance and advancing, returning, throwing, or canceling its execution.

Parameters:
Name Type Description
state YieldableState
Source:

then() → {Promise}

Returns a promise that resolves with the value yielded back to or returned to the yielded task, or rejects with either the exception thrown from the Yieldable, or an error with a .name property with value "TaskCancelation".

Source:
Returns:
Type
Promise