MetaphorJs.lib.Promise

Bare bones Promises/A+ implementation / ES6 Promise polyfill

1.1.0

class MetaphorJs.lib.Promise

Methods
  • constructor

    Promise (fn, context)  -> Promise

    • function fn (resolve, reject)

      Constructor accepts two parameters: resolve and reject functions.

      • function resolve (value)
        • * value
      • function reject (reason)
        • * reason
    • object context
  • constructor

    Promise (thenable)  -> Promise

    • Thenable thenable
  • constructor

    Promise (value)  -> Promise

    • * value

      Value to resolve promise with

  • constructor

    Promise ()  -> Promise

  • isPending ()  -> boolean

    Is promise still pending (as opposed to resolved or rejected)

  • isFulfilled ()  -> boolean

    Is the promise fulfilled. Same as isResolved()

  • isResolved ()  -> boolean

    Is the promise resolved. Same as isFulfilled()

  • isRejected ()  -> boolean

    Is the promise rejected

  • isCancelled ()  -> boolean

    Is the promise was destroyed before resolving or rejecting

  • hasListeners ()  -> boolean

    Did someone subscribed to this promise

  • resolve (value)

    Resolve the promise

    • * value
  • reject (reason)

    Reject the promise

    • * reason
  • async

    then (resolve, reject, context)  -> Promise

    • function resolve ()

      -- called when this promise is resolved; returns new resolve value or promise

    • function reject ()

      -- called when this promise is rejected; returns new reject reason

    • object context

      -- resolve's and reject's functions "this" object

    -> {Promise} new promise
  • async

    catch (reject)  -> Promise

    Add reject listener.

    • function reject ()

      -- same as then(null, reject)

    -> {Promise} new promise
  • sync

    done (fn, context)  -> Promise

    Add resolve listener

    • function fn ()

      -- function to call when promise is resolved

    • object context

      -- function's "this" object

    -> {Promise} same promise
  • sync

    fail (fn, context)  -> Promise

    Add reject listener

    • function fn ()

      -- function to call when promise is rejected.

    • object context

      -- function's "this" object

    -> {Promise} same promise
  • sync

    always (fn, context)  -> Promise

    Add both resolve and reject listener

    • function fn ()

      -- function to call when promise resolved or rejected

    • object context

      -- function's "this" object

    -> {Promise} same promise
  • promise ()  -> object

    Get a thenable object

    -> {object} then: function, done: function, fail: function, always: function
  • after (value)  -> Promise

    Resolve this promise after value promise is resolved.

    • * | Promise value
    -> {Promise} self
  • static

    fcall (fn, context, args)  -> Promise

    Call function fn with given args in given context and use its return value as resolve value for a new promise. Then return this promise.

    • function fn ()
    • object context
    • array args
  • static

    resolve (value)  -> Promise

    Create new promise and resolve it with given value

    • * value
  • static

    reject (reason)  -> Promise

    Create new promise and reject it with given reason

    • * reason
  • static

    all (promises)  -> Promise

    Take a list of promises or values and once all promises are resolved, create a new promise and resolve it with a list of final values.
    If one of the promises is rejected, it will reject the returned promise.

    • array promises

      -- array of promises or resolve values

  • static

    when (promise1, promise2, promiseN)  -> Promise

    Same as all() but it treats arguments as list of values.

    • Promise | * promise1
    • Promise | * promise2
    • Promise | * promiseN
  • static

    allResolved (promises)  -> Promise

    Same as all() but the resulting promise will not be rejected if ones of the passed promises is rejected.

    • array promises

      -- array of promises or resolve values

  • static

    race (promises)  -> Promise

    Given the list of promises or values it will return a new promise and resolve it with the first resolved value.

    • array promises

      -- array of promises or resolve values

  • static

    waterfall (functions)  -> Promise

    Takes a list of async functions and executes them in given order consequentially

    • array functions

      -- array of promises or resolve values or functions

  • static

    forEach (items, fn, context, allResolved)

    Works like Array.forEach but it expects passed function to return a Promise.

    • array items
    • function fn (value, index)  -> Promise | *
      • * value
      • int index
    • object context
    • boolean allResolved

      if true, the resulting promise will fail if one of the returned promises fails.

  • static

    counter (cnt)  -> Promise

    Returns a promise with additional countdown method. Call this method cnt times and the promise will get resolved.

    • int cnt

mixin MetaphorJs.mixin.Promise

Methods
  • async

    then (resolve, reject, context)  -> Promise

    • function resolve ()

      -- called when this promise is resolved; returns new resolve value or promise

    • function reject ()

      -- called when this promise is rejected; returns new reject reason

    • object context

      -- resolve's and reject's functions "this" object

    -> {Promise} new promise
  • sync

    done (fn, context)  -> Promise

    Add resolve listener

    • function fn ()

      -- function to call when promise is resolved

    • object context

      -- function's "this" object

    -> {Promise} same promise
  • sync

    always (fn, context)  -> Promise

    Add both resolve and reject listener

    • function fn ()

      -- function to call when promise resolved or rejected

    • object context

      -- function's "this" object

    -> {Promise} same promise
  • sync

    fail (fn, context)  -> Promise

    Add reject listener

    • function fn ()

      -- function to call when promise is rejected.

    • object context

      -- function's "this" object

    -> {Promise} same promise