Class: SyncPromise

SyncPromise

new SyncPromise(value, isRejected)

A SyncPromise is a promise which is immediately fulfilled or rejected, used to return a promise in synchronous code. This private constructor creates a SyncPromise fulfilled or rejected with the given value. You should normally not call this constructor but call SyncPromise.resolve or SyncPromise.reject. Note that we don't need a constructor like SyncPromise(function(resolve, reject)) because this would be for scheduling the function to be called later, which we don't do.
Parameters:
Name Type Description
value any If isRejected is false, this is the value of the fulfilled promise, else if isRejected is true, this is the error.
isRejected boolean True to create a promise in the rejected state, where value is the error.
Source:

Methods

(static) complete(onComplete, onError, promise) → {any}

This can be called with complete(onComplete, promise) or complete(onComplete, onError, promise) to handle both synchronous and asynchronous code based on whether the caller supplies the onComlete callback. If onComplete is defined, call promise.then with a function which calls onComplete(value) when fulfilled (possibly in asynchronous mode). If onComplete is undefined, then we are in synchronous mode so return SyncPromise.getValue(promise) which will throw an exception if the promise is not a SyncPromise (or is a SyncPromise in the rejected state).
Parameters:
Name Type Description
onComplete function If defined, this calls promise.then to fulfill the promise, then calls onComplete(value) with the value of the promise. If onComplete is undefined, the return value is described below. NOTE: The library will log any exceptions thrown by this callback, but for better error handling the callback should catch and properly handle any exceptions.
onError function (optional) If defined, then onComplete must be defined and if there is an error when this calls promise.then, this calls onError(err) with the value of the error. If onComplete is undefined, then onError is ignored and this will call SyncPromise.getValue(promise) which may throw an exception. NOTE: The library will log any exceptions thrown by this callback, but for better error handling the callback should catch and properly handle any exceptions.
promise Promise | SyncPromise If onComplete is defined, this calls promise.then. Otherwise, this calls SyncPromise.getValue(promise).
Source:
Throws:
  • Error If onComplete is undefined and promise is not a SyncPromise.
  • any If onComplete is undefined and promise is a SyncPromise in the rejected state.
Returns:
If onComplete is undefined, return SyncPromise.getValue(promise). Otherwise, if onComplete is supplied then return undefined and use onComplete as described above.
Type
any

(static) getValue(promise) → {any}

This static method checks if the promise is a SyncPromise and immediately returns its value or throws the error if promise is rejected. If promise is not a SyncPromise, this throws an exception since it is not possible to immediately get the value. This can be used with "promise-based" code which you expect to always return a SyncPromise to operate in synchronous mode.
Parameters:
Name Type Description
promise SyncPromise The SyncPromise with the value to get.
Source:
Throws:
  • Error If promise is not a SyncPromise.
  • any If promise is a SyncPromise in the rejected state, this throws the error.
Returns:
The value of the promise.
Type
any

(static) reject(err)

Return a new SyncPromise which is already rejected with the given error.
Parameters:
Name Type Description
err any The error for the rejected promise.
Source:

(static) resolve(value)

Return a new SyncPromise which is already fulfilled to the given value.
Parameters:
Name Type Description
value any The value of the promise.
Source:

catch()

Call this.then(undefined, onRejected) and return the result. If this promise is rejected then onRejected will process it. If this promise is fulfilled, this simply passes it forward.
Source:

then((optional), (optional)) → {Promise|SyncPromise}

If this promise is fulfilled, immediately call onFulfilled with the fulfilled value as described below. Otherwise, if this promise is rejected, immediately call onRejected with the error as described below.
Parameters:
Name Type Description
(optional) function onFulfilled If this promise is fulfilled, this calls onFulfilled(value) with the value of this promise and returns the result. The function should return a promise. To use all synchronous code, onFulfilled should return SyncPromise.resolve(newValue).
(optional) function onRejected If this promise is rejected, this calls onRejected(err) with the error value of this promise and returns the result. The function should return a promise. To use all synchronous code, onFulfilled should return SyncPromise.resolve(newValue) (or throw an exception).
Source:
Returns:
If this promise is fulfilled, return the result of calling onFulfilled(value). Note that this does not create a promise which is scheduled to execute later. Rather it immediately calls onFulfilled which should return a promise. But if onFulfilled is undefined, simply return this promise to pass it forward. If this promise is rejected, return the result of calling onRejected(err) with the error value. But if onRejected is undefined, simply return this promise to pass it forward. However, if onFulfilled or onRejected throws an exception, then return a new SyncPromise in the rejected state with the exception.
Type
Promise | SyncPromise