Promise

Description: For a detailed explanation of how Result and Promise work, see this blog post. But keep in mind that the API has changed since that was written. The Match(), MatchVal() is replaced with Then() and MatchErr() is replaced with Catch().

A Promise represents a value that may not exist yet. This is designed to function very much like a JavaScript promise, even mirroring its API as closely as possible. Think of it as an asynchronous Result. The difference is that when you give it a callback, it won't be called until the Promise is resolved. If the Promise is already resolved, they'll be called immediately.

Constructors:

  • Promise() - Create an unresolved Promise.
  • Promise(Result<ValueT> value) - Create a resolved Promise with the same value or Error as the Result passed in.
  • Promise(Promise<ValueT> promise) - Create a Promise that copies the other Promise. If the other Promise is already resolved, this one will also be immediately resolved. If it's not, it will resolve when the other one does.

Methods:

  • static Promise<ValueT> Val(ValueT val) - This factory function will make a new resolved Promise with the passed in value.
  • static Promise<ValueT> Err(Errorerr) - This factory function will make a new resolved Promise with the passed in Error.
  • static Promise<List<ValueT>> All(List<Promise<ValueT>> promises) - This static function will take a list of Promises of the same type, and return a Promise of a list of the resulting values. If any of the Promises in the list resole to an Error, this Promise will resolve to the same Error.
  • static static Promise<Tuple2<ValueA, ValueB>> All<ValueA, ValueB>(Promise<ValueA> promise1, Promise<ValueB> promise2) - This static function will take two Promises, and return a Promise of a Tuple2 containing the resulting values of those Promises. If either of the Promises resolve to an Error, this Promise will resolve to the same Error.
  • static static Promise<Tuple3<ValueA, ValueB, ValueC>> All<ValueA, ValueB, ValueC>(Promise<ValueA> promise1, Promise<ValueB> promise2, Promise<ValueC> promise3) - This static function will take three Promises, and return a Promise of a Tuple3 containing the resulting values of those Promises. If any of the Promises resolve to an Error, this Promise will resolve to the same Error.
  • void Then(Action<ValueT> valFunc, Action<Error> errFunc) - When the Promise resolves, call the valFunc if it resolves with a value, or the errFunc if it resolves with an Error. If the Promise is already resolved, call the appropriate callback immediately.
  • void Then(Action<ValueT> valFunc) - When the Promise resolves, call the valFunc if it resolves with a value. If the Promise is already resolved with a value, call the callback immediately.
  • Promise<NewValueT> Then<NewValueT>(Func<ValueT,Result<NewValueT>> valFunc, Func<Error,Result<NewValueT>> errFunc) - Returns a new Promise immediately. When this Promise is resolved, call the valFunc if it resolves with a value, or the errFunc if it resolves with an Error, and resolve the returned Promise with the return value of whichever one was called.
  • Promise<NewValueT> Then<NewValueT>(Func<ValueT,Result<NewValueT>> valFunc) - Returns a new Promise immediately. When this Promise is resolved, call the valFunc if it resolves with a value, and resolve the returned Promise with it's return value. If this Promise resolves with an Error, resolve the returned Promise with the same Error.
  • Promise<NewValueT> Then<NewValueT>(Func<ValueT, Promise<NewValueT>> valFunc, Func<Error, Promise<NewValueT>> errFunc) - Returns a new Promise immediately. When this Promise is resolved, call the valFunc if it resolves with a value, or the errFunc if it resolves with an Error, and resolve the returned Promise when the Promise returned by the callback is resolved.
  • Promise<NewValueT> Then<NewValueT>(Func<ValueT, Promise<NewValueT>> valFunc) - Returns a new Promise immediately. When this Promise is resolved, call the valFunc if it resolves with a value, and resolve the returned Promise when the Promise returned by the callback resolves, if it resolves with a value. If it resolves with an Error, resolve the returned Promise with the same Error.
  • void Catch(Action<Error> errFunc) - When the Promise resolves, call the errFunc if it resolves with an Error. If the Promise is already resolved with an Error, call the callback immediately.
  • Promise<ValueT> Catch(Func<Error,Result<ValueT>> errFunc) - Return a new Promise immediately. When this Promise is resolved, call errFunc if it resolves with an Error, and resolve the returned Promise with the result value. If this Promise resolves with a value, resolve the returned Promise with the same value.
  • Promise<ValueT> Catch(Func<Error, Promise<ValueT>> errFunc) - Return a new Promise immediately. When this Promise is resolved, call errFunc if it resolves with an Error, and resolve the returned Promise when the Promise returned by the callback resolves, if it resolves with an Error. If it resolves with a value, resolve the returned Promise with the same value.
  • void Resolve(Result<ValueT> result) - Resolve this Promise.
  • void Accept(ValueT acceptValue) - Resolve this Promise with the given value.
  • void Reject(ErrorrejectValue) - Resolve this Promise with the given Error.