Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Promise<T>

Promises are used to do asynchronous work and they are useful for creating a chain of actions. In Excalibur they are used for loading, sounds, animation, actions, and more.

A Promise Chain

Promises can be chained together and can be useful for creating a queue of functions to be called when something is done. The first Promise you will encounter is probably Engine.start which resolves when the game has finished loading.

var game = new ex.Engine();
// perform start-up logic once game is ready
game.start().then(function() {
  // start-up & initialization logic
});

Handling errors

You can optionally pass an error handler to Promise.then which will handle any errors that occur during Promise execution.

var game = new ex.Engine();
game.start().then(
  // success handler
  function() {},
  // error handler
  function(err) {}
);

Any errors that go unhandled will be bubbled up to the browser.

Type parameters

  • T

Hierarchy

  • Promise

Implements

Index

Methods

error

  • error(errorCallback?: function): this
  • Add an error callback to the promise

    Parameters

    • Optional errorCallback: function

      Call if there was an error in a callback

        • (value?: any): any
        • Parameters

          • Optional value: any

          Returns any

    Returns this

reject

  • reject(value?: any): this
  • Reject the promise and pass an option value to the reject callbacks

    Parameters

    • Optional value: any

      Value to pass to the reject callbacks

    Returns this

resolve

  • Resolve the promise and pass an option value to the success callbacks

    Parameters

    • Optional value: T

      Value to pass to the success callbacks

    Returns Promise<T>

state

then

  • then(successCallback?: function, rejectCallback?: function): this
  • Chain success and reject callbacks after the promise is resolved

    Parameters

    • Optional successCallback: function

      Call on resolution of promise

        • (value?: T): any
        • Parameters

          • Optional value: T

          Returns any

    • Optional rejectCallback: function

      Call on rejection of promise

        • (value?: any): any
        • Parameters

          • Optional value: any

          Returns any

    Returns this

Static join

  • Returns a new promise that resolves when all the promises passed to it resolve, or rejects when at least 1 promise rejects.

    Type parameters

    • T

    Parameters

    Returns Promise<T>

  • Returns a new promise that resolves when all the promises passed to it resolve, or rejects when at least 1 promise rejects.

    Type parameters

    • T

    Parameters

    Returns Promise<T>

Static reject

  • Create and reject a Promise with an optional value

    Type parameters

    • T

    Parameters

    • Optional value: T

      An optional value to wrap in a rejected promise

    Returns Promise<T>

Static resolve

  • resolve<T>(value?: T): Promise<T>
  • Create and resolve a Promise with an optional value

    Type parameters

    • T

    Parameters

    • Optional value: T

      An optional value to wrap in a resolved promise

    Returns Promise<T>