Class Promise | Package Manager UI website
docs.unity3d.com
    Show / Hide Table of Contents

    Class Promise

    Implements a non-generic C# promise, this is a promise that simply resolves without delivering a value. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

    Inheritance
    System.Object
    Promise
    TickerFutureImpl
    Namespace: RSG
    Syntax
    public class Promise : IPromise, IPendingPromise, IRejectable, IPromiseInfo

    Constructors

    Promise(Action<Action, Action<Exception>>, Boolean)

    Declaration
    public Promise(Action<Action, Action<Exception>> resolver, bool isSync = false)
    Parameters
    Type Name Description
    System.Action<System.Action, System.Action<System.Exception>> resolver
    System.Boolean isSync

    Promise(Boolean)

    Declaration
    public Promise(bool isSync = false)
    Parameters
    Type Name Description
    System.Boolean isSync

    Fields

    EnablePromiseTracking

    Set to true to enable tracking of promises.

    Declaration
    public static bool EnablePromiseTracking
    Field Value
    Type Description
    System.Boolean

    Properties

    CurState

    Tracks the current state of the promise.

    Declaration
    public PromiseState CurState { get; }
    Property Value
    Type Description
    PromiseState

    Id

    ID of the promise, useful for debugging.

    Declaration
    public int Id { get; }
    Property Value
    Type Description
    System.Int32
    Implements
    IPromise.Id
    IPendingPromise.Id
    IPromiseInfo.Id

    IsSync

    Declaration
    public bool IsSync { get; }
    Property Value
    Type Description
    System.Boolean

    Name

    Name of the promise, when set, useful for debugging.

    Declaration
    public string Name { get; }
    Property Value
    Type Description
    System.String
    Implements
    IPromiseInfo.Name

    Methods

    All(IPromise[])

    Returns a promise that resolves when all of the promises in the enumerable argument have resolved. Returns a promise of a collection of the resolved results.

    Declaration
    public static IPromise All(params IPromise[] promises)
    Parameters
    Type Name Description
    IPromise[] promises
    Returns
    Type Description
    IPromise

    All(IEnumerable<IPromise>)

    Returns a promise that resolves when all of the promises in the enumerable argument have resolved. Returns a promise of a collection of the resolved results.

    Declaration
    public static IPromise All(IEnumerable<IPromise> promises)
    Parameters
    Type Name Description
    System.Collections.Generic.IEnumerable<IPromise> promises
    Returns
    Type Description
    IPromise

    Catch(Action<Exception>)

    Handle errors for the promise.

    Declaration
    public IPromise Catch(Action<Exception> onRejected)
    Parameters
    Type Name Description
    System.Action<System.Exception> onRejected
    Returns
    Type Description
    IPromise
    Implements
    IPromise.Catch(Action<Exception>)

    ContinueWith(Func<IPromise>)

    Declaration
    public IPromise ContinueWith(Func<IPromise> onComplete)
    Parameters
    Type Name Description
    System.Func<IPromise> onComplete
    Returns
    Type Description
    IPromise
    Implements
    IPromise.ContinueWith(Func<IPromise>)

    ContinueWith<ConvertedT>(Func<IPromise<ConvertedT>>)

    Declaration
    public IPromise<ConvertedT> ContinueWith<ConvertedT>(Func<IPromise<ConvertedT>> onComplete)
    Parameters
    Type Name Description
    System.Func<IPromise<ConvertedT>> onComplete
    Returns
    Type Description
    IPromise<ConvertedT>
    Type Parameters
    Name Description
    ConvertedT
    Implements
    IPromise.ContinueWith<ConvertedT>(Func<IPromise<ConvertedT>>)

    Delayed(TimeSpan)

    Declaration
    public static IPromise Delayed(TimeSpan duration)
    Parameters
    Type Name Description
    System.TimeSpan duration
    Returns
    Type Description
    IPromise

    Done()

    Complete the promise. Adds a defualt error handler.

    Declaration
    public void Done()
    Implements
    IPromise.Done()

    Done(Action)

    Completes the promise. onResolved is called on successful completion. Adds a default error handler.

    Declaration
    public void Done(Action onResolved)
    Parameters
    Type Name Description
    System.Action onResolved
    Implements
    IPromise.Done(Action)

    Done(Action, Action<Exception>)

    Completes the promise. onResolved is called on successful completion. onRejected is called on error.

    Declaration
    public void Done(Action onResolved, Action<Exception> onRejected)
    Parameters
    Type Name Description
    System.Action onResolved
    System.Action<System.Exception> onRejected
    Implements
    IPromise.Done(Action, Action<Exception>)

    Finally(Action)

    Declaration
    public IPromise Finally(Action onComplete)
    Parameters
    Type Name Description
    System.Action onComplete
    Returns
    Type Description
    IPromise
    Implements
    IPromise.Finally(Action)

    GetPendingPromises()

    Information about pending promises, useful for debugging. This is only populated when 'EnablePromiseTracking' is set to true.

    Declaration
    public static IEnumerable<IPromiseInfo> GetPendingPromises()
    Returns
    Type Description
    System.Collections.Generic.IEnumerable<IPromiseInfo>

    Progress(Action<Single>)

    Declaration
    public IPromise Progress(Action<float> onProgress)
    Parameters
    Type Name Description
    System.Action<System.Single> onProgress
    Returns
    Type Description
    IPromise
    Implements
    IPromise.Progress(Action<Single>)

    Race(IPromise[])

    Returns a promise that resolves when the first of the promises in the enumerable argument have resolved. Returns the value from the first promise that has resolved.

    Declaration
    public static IPromise Race(params IPromise[] promises)
    Parameters
    Type Name Description
    IPromise[] promises
    Returns
    Type Description
    IPromise

    Race(IEnumerable<IPromise>)

    Returns a promise that resolves when the first of the promises in the enumerable argument have resolved. Returns the value from the first promise that has resolved.

    Declaration
    public static IPromise Race(IEnumerable<IPromise> promises)
    Parameters
    Type Name Description
    System.Collections.Generic.IEnumerable<IPromise> promises
    Returns
    Type Description
    IPromise

    Reject(Exception)

    Reject the promise with an exception.

    Declaration
    public void Reject(Exception ex)
    Parameters
    Type Name Description
    System.Exception ex
    Implements
    IRejectable.Reject(Exception)

    Rejected(Exception)

    Convert an exception directly into a rejected promise.

    Declaration
    public static IPromise Rejected(Exception ex)
    Parameters
    Type Name Description
    System.Exception ex
    Returns
    Type Description
    IPromise

    RejectSync(Exception)

    Declaration
    public void RejectSync(Exception ex)
    Parameters
    Type Name Description
    System.Exception ex

    ReportProgress(Single)

    Report progress on the promise.

    Declaration
    public void ReportProgress(float progress)
    Parameters
    Type Name Description
    System.Single progress
    Implements
    IPendingPromise.ReportProgress(Single)

    Resolve()

    Resolve the promise with a particular value.

    Declaration
    public void Resolve()
    Implements
    IPendingPromise.Resolve()

    Resolved()

    Convert a simple value directly into a resolved promise.

    Declaration
    public static IPromise Resolved()
    Returns
    Type Description
    IPromise

    ResolveSync()

    Declaration
    public void ResolveSync()

    Sequence(IEnumerable<Func<IPromise>>)

    Chain a sequence of operations using promises. Takes a collection of functions each of which starts an async operation and yields a promise.

    Declaration
    public static IPromise Sequence(IEnumerable<Func<IPromise>> fns)
    Parameters
    Type Name Description
    System.Collections.Generic.IEnumerable<System.Func<IPromise>> fns
    Returns
    Type Description
    IPromise

    Sequence(Func<IPromise>[])

    Chain a number of operations using promises. Takes a number of functions each of which starts an async operation and yields a promise.

    Declaration
    public static IPromise Sequence(params Func<IPromise>[] fns)
    Parameters
    Type Name Description
    System.Func<IPromise>[] fns
    Returns
    Type Description
    IPromise

    Then(Action)

    Add a resolved callback.

    Declaration
    public IPromise Then(Action onResolved)
    Parameters
    Type Name Description
    System.Action onResolved
    Returns
    Type Description
    IPromise
    Implements
    IPromise.Then(Action)

    Then(Action, Action<Exception>)

    Add a resolved callback and a rejected callback.

    Declaration
    public IPromise Then(Action onResolved, Action<Exception> onRejected)
    Parameters
    Type Name Description
    System.Action onResolved
    System.Action<System.Exception> onRejected
    Returns
    Type Description
    IPromise
    Implements
    IPromise.Then(Action, Action<Exception>)

    Then(Action, Action<Exception>, Action<Single>)

    Add a resolved callback, a rejected callback and a progress callback.

    Declaration
    public IPromise Then(Action onResolved, Action<Exception> onRejected, Action<float> onProgress)
    Parameters
    Type Name Description
    System.Action onResolved
    System.Action<System.Exception> onRejected
    System.Action<System.Single> onProgress
    Returns
    Type Description
    IPromise
    Implements
    IPromise.Then(Action, Action<Exception>, Action<Single>)

    Then(Func<IPromise>)

    Add a resolved callback that chains a non-value promise.

    Declaration
    public IPromise Then(Func<IPromise> onResolved)
    Parameters
    Type Name Description
    System.Func<IPromise> onResolved
    Returns
    Type Description
    IPromise
    Implements
    IPromise.Then(Func<IPromise>)

    Then(Func<IPromise>, Action<Exception>)

    Add a resolved callback and a rejected callback. The resolved callback chains a non-value promise.

    Declaration
    public IPromise Then(Func<IPromise> onResolved, Action<Exception> onRejected)
    Parameters
    Type Name Description
    System.Func<IPromise> onResolved
    System.Action<System.Exception> onRejected
    Returns
    Type Description
    IPromise
    Implements
    IPromise.Then(Func<IPromise>, Action<Exception>)

    Then(Func<IPromise>, Action<Exception>, Action<Single>)

    Add a resolved callback, a rejected callback and a progress callback. The resolved callback chains a non-value promise.

    Declaration
    public IPromise Then(Func<IPromise> onResolved, Action<Exception> onRejected, Action<float> onProgress)
    Parameters
    Type Name Description
    System.Func<IPromise> onResolved
    System.Action<System.Exception> onRejected
    System.Action<System.Single> onProgress
    Returns
    Type Description
    IPromise
    Implements
    IPromise.Then(Func<IPromise>, Action<Exception>, Action<Single>)

    Then<ConvertedT>(Func<IPromise<ConvertedT>>)

    Add a resolved callback that chains a value promise (optionally converting to a different value type).

    Declaration
    public IPromise<ConvertedT> Then<ConvertedT>(Func<IPromise<ConvertedT>> onResolved)
    Parameters
    Type Name Description
    System.Func<IPromise<ConvertedT>> onResolved
    Returns
    Type Description
    IPromise<ConvertedT>
    Type Parameters
    Name Description
    ConvertedT
    Implements
    IPromise.Then<ConvertedT>(Func<IPromise<ConvertedT>>)

    Then<ConvertedT>(Func<IPromise<ConvertedT>>, Func<Exception, IPromise<ConvertedT>>)

    Add a resolved callback and a rejected callback. The resolved callback chains a value promise (optionally converting to a different value type).

    Declaration
    public IPromise<ConvertedT> Then<ConvertedT>(Func<IPromise<ConvertedT>> onResolved, Func<Exception, IPromise<ConvertedT>> onRejected)
    Parameters
    Type Name Description
    System.Func<IPromise<ConvertedT>> onResolved
    System.Func<System.Exception, IPromise<ConvertedT>> onRejected
    Returns
    Type Description
    IPromise<ConvertedT>
    Type Parameters
    Name Description
    ConvertedT
    Implements
    IPromise.Then<ConvertedT>(Func<IPromise<ConvertedT>>, Func<Exception, IPromise<ConvertedT>>)

    Then<ConvertedT>(Func<IPromise<ConvertedT>>, Func<Exception, IPromise<ConvertedT>>, Action<Single>)

    Add a resolved callback, a rejected callback and a progress callback. The resolved callback chains a value promise (optionally converting to a different value type).

    Declaration
    public IPromise<ConvertedT> Then<ConvertedT>(Func<IPromise<ConvertedT>> onResolved, Func<Exception, IPromise<ConvertedT>> onRejected, Action<float> onProgress)
    Parameters
    Type Name Description
    System.Func<IPromise<ConvertedT>> onResolved
    System.Func<System.Exception, IPromise<ConvertedT>> onRejected
    System.Action<System.Single> onProgress
    Returns
    Type Description
    IPromise<ConvertedT>
    Type Parameters
    Name Description
    ConvertedT
    Implements
    IPromise.Then<ConvertedT>(Func<IPromise<ConvertedT>>, Func<Exception, IPromise<ConvertedT>>, Action<Single>)

    ThenAll(Func<IEnumerable<IPromise>>)

    Chain an enumerable of promises, all of which must resolve. The resulting promise is resolved when all of the promises have resolved. It is rejected as soon as any of the promises have been rejected.

    Declaration
    public IPromise ThenAll(Func<IEnumerable<IPromise>> chain)
    Parameters
    Type Name Description
    System.Func<System.Collections.Generic.IEnumerable<IPromise>> chain
    Returns
    Type Description
    IPromise
    Implements
    IPromise.ThenAll(Func<IEnumerable<IPromise>>)

    ThenAll<ConvertedT>(Func<IEnumerable<IPromise<ConvertedT>>>)

    Chain an enumerable of promises, all of which must resolve. Converts to a non-value promise. The resulting promise is resolved when all of the promises have resolved. It is rejected as soon as any of the promises have been rejected.

    Declaration
    public IPromise<IEnumerable<ConvertedT>> ThenAll<ConvertedT>(Func<IEnumerable<IPromise<ConvertedT>>> chain)
    Parameters
    Type Name Description
    System.Func<System.Collections.Generic.IEnumerable<IPromise<ConvertedT>>> chain
    Returns
    Type Description
    IPromise<System.Collections.Generic.IEnumerable<ConvertedT>>
    Type Parameters
    Name Description
    ConvertedT
    Implements
    IPromise.ThenAll<ConvertedT>(Func<IEnumerable<IPromise<ConvertedT>>>)

    ThenRace(Func<IEnumerable<IPromise>>)

    Takes a function that yields an enumerable of promises. Returns a promise that resolves when the first of the promises has resolved.

    Declaration
    public IPromise ThenRace(Func<IEnumerable<IPromise>> chain)
    Parameters
    Type Name Description
    System.Func<System.Collections.Generic.IEnumerable<IPromise>> chain
    Returns
    Type Description
    IPromise
    Implements
    IPromise.ThenRace(Func<IEnumerable<IPromise>>)

    ThenRace<ConvertedT>(Func<IEnumerable<IPromise<ConvertedT>>>)

    Takes a function that yields an enumerable of promises. Converts to a value promise. Returns a promise that resolves when the first of the promises has resolved.

    Declaration
    public IPromise<ConvertedT> ThenRace<ConvertedT>(Func<IEnumerable<IPromise<ConvertedT>>> chain)
    Parameters
    Type Name Description
    System.Func<System.Collections.Generic.IEnumerable<IPromise<ConvertedT>>> chain
    Returns
    Type Description
    IPromise<ConvertedT>
    Type Parameters
    Name Description
    ConvertedT
    Implements
    IPromise.ThenRace<ConvertedT>(Func<IEnumerable<IPromise<ConvertedT>>>)

    ThenSequence(Func<IEnumerable<Func<IPromise>>>)

    Chain a sequence of operations using promises. Reutrn a collection of functions each of which starts an async operation and yields a promise. Each function will be called and each promise resolved in turn. The resulting promise is resolved after each promise is resolved in sequence.

    Declaration
    public IPromise ThenSequence(Func<IEnumerable<Func<IPromise>>> chain)
    Parameters
    Type Name Description
    System.Func<System.Collections.Generic.IEnumerable<System.Func<IPromise>>> chain
    Returns
    Type Description
    IPromise
    Implements
    IPromise.ThenSequence(Func<IEnumerable<Func<IPromise>>>)

    WithName(String)

    Set the name of the promise, useful for debugging.

    Declaration
    public IPromise WithName(string name)
    Parameters
    Type Name Description
    System.String name
    Returns
    Type Description
    IPromise
    Implements
    IPromise.WithName(String)

    Events

    UnhandledException

    Event raised for unhandled errors. For this to work you have to complete your promises with a call to Done().

    Declaration
    public static event EventHandler<ExceptionEventArgs> UnhandledException
    Event Type
    Type Description
    System.EventHandler<ExceptionEventArgs>
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023