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

    Class Promise<PromisedT>

    Implements a C# promise. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

    Inheritance
    System.Object
    Promise<PromisedT>
    Namespace: RSG
    Syntax
    public class Promise<PromisedT> : IPromise<PromisedT>, IPendingPromise<PromisedT>, IRejectable, IPromiseInfo
    Type Parameters
    Name Description
    PromisedT

    Constructors

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

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

    Promise(Boolean)

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

    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<PromisedT>.Id
    IPendingPromise<PromisedT>.Id
    IPromiseInfo.Id

    isCompleted

    Declaration
    public bool isCompleted { get; }
    Property Value
    Type Description
    System.Boolean
    Implements
    IPromise<PromisedT>.isCompleted

    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<PromisedT>[])

    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<IEnumerable<PromisedT>> All(params IPromise<PromisedT>[] promises)
    Parameters
    Type Name Description
    IPromise<PromisedT>[] promises
    Returns
    Type Description
    IPromise<System.Collections.Generic.IEnumerable<PromisedT>>

    All(IEnumerable<IPromise<PromisedT>>)

    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<IEnumerable<PromisedT>> All(IEnumerable<IPromise<PromisedT>> promises)
    Parameters
    Type Name Description
    System.Collections.Generic.IEnumerable<IPromise<PromisedT>> promises
    Returns
    Type Description
    IPromise<System.Collections.Generic.IEnumerable<PromisedT>>

    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<PromisedT>.Catch(Action<Exception>)

    Catch(Func<Exception, PromisedT>)

    Handle errors for the promise.

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

    ContinueWith(Func<IPromise>)

    Declaration
    public IPromise ContinueWith(Func<IPromise> onComplete)
    Parameters
    Type Name Description
    System.Func<IPromise> onComplete
    Returns
    Type Description
    IPromise
    Implements
    IPromise<PromisedT>.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<PromisedT>.ContinueWith<ConvertedT>(Func<IPromise<ConvertedT>>)

    Done()

    Complete the promise. Adds a default error handler.

    Declaration
    public void Done()
    Implements
    IPromise<PromisedT>.Done()

    Done(Action<PromisedT>)

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

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

    Done(Action<PromisedT>, Action<Exception>)

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

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

    Finally(Action)

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

    Progress(Action<Single>)

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

    Race(IPromise<PromisedT>[])

    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<PromisedT> Race(params IPromise<PromisedT>[] promises)
    Parameters
    Type Name Description
    IPromise<PromisedT>[] promises
    Returns
    Type Description
    IPromise<PromisedT>

    Race(IEnumerable<IPromise<PromisedT>>)

    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<PromisedT> Race(IEnumerable<IPromise<PromisedT>> promises)
    Parameters
    Type Name Description
    System.Collections.Generic.IEnumerable<IPromise<PromisedT>> promises
    Returns
    Type Description
    IPromise<PromisedT>

    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<PromisedT> Rejected(Exception ex)
    Parameters
    Type Name Description
    System.Exception ex
    Returns
    Type Description
    IPromise<PromisedT>

    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<PromisedT>.ReportProgress(Single)

    Resolve(PromisedT)

    Resolve the promise with a particular value.

    Declaration
    public void Resolve(PromisedT value)
    Parameters
    Type Name Description
    PromisedT value
    Implements
    IPendingPromise<PromisedT>.Resolve(PromisedT)

    Resolved(PromisedT)

    Convert a simple value directly into a resolved promise.

    Declaration
    public static IPromise<PromisedT> Resolved(PromisedT promisedValue)
    Parameters
    Type Name Description
    PromisedT promisedValue
    Returns
    Type Description
    IPromise<PromisedT>

    ResolveSync(PromisedT)

    Declaration
    public void ResolveSync(PromisedT value)
    Parameters
    Type Name Description
    PromisedT value

    Then(Action<PromisedT>)

    Add a resolved callback.

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

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

    Add a resolved callback and a rejected callback.

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

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

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

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

    Then(Func<PromisedT, IPromise>)

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

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

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

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

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

    Then(Func<PromisedT, 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<PromisedT, IPromise> onResolved, Action<Exception> onRejected, Action<float> onProgress)
    Parameters
    Type Name Description
    System.Func<PromisedT, IPromise> onResolved
    System.Action<System.Exception> onRejected
    System.Action<System.Single> onProgress
    Returns
    Type Description
    IPromise
    Implements
    IPromise<PromisedT>.Then(Func<PromisedT, IPromise>, Action<Exception>, Action<Single>)

    Then<ConvertedT>(Func<PromisedT, ConvertedT>)

    Return a new promise with a different value. May also change the type of the value.

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

    Then<ConvertedT>(Func<PromisedT, 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<PromisedT, IPromise<ConvertedT>> onResolved)
    Parameters
    Type Name Description
    System.Func<PromisedT, IPromise<ConvertedT>> onResolved
    Returns
    Type Description
    IPromise<ConvertedT>
    Type Parameters
    Name Description
    ConvertedT
    Implements
    IPromise<PromisedT>.Then<ConvertedT>(Func<PromisedT, IPromise<ConvertedT>>)

    Then<ConvertedT>(Func<PromisedT, 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<PromisedT, IPromise<ConvertedT>> onResolved, Func<Exception, IPromise<ConvertedT>> onRejected)
    Parameters
    Type Name Description
    System.Func<PromisedT, IPromise<ConvertedT>> onResolved
    System.Func<System.Exception, IPromise<ConvertedT>> onRejected
    Returns
    Type Description
    IPromise<ConvertedT>
    Type Parameters
    Name Description
    ConvertedT
    Implements
    IPromise<PromisedT>.Then<ConvertedT>(Func<PromisedT, IPromise<ConvertedT>>, Func<Exception, IPromise<ConvertedT>>)

    Then<ConvertedT>(Func<PromisedT, 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<PromisedT, IPromise<ConvertedT>> onResolved, Func<Exception, IPromise<ConvertedT>> onRejected, Action<float> onProgress)
    Parameters
    Type Name Description
    System.Func<PromisedT, 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<PromisedT>.Then<ConvertedT>(Func<PromisedT, IPromise<ConvertedT>>, Func<Exception, IPromise<ConvertedT>>, Action<Single>)

    ThenAll(Func<PromisedT, IEnumerable<IPromise>>)

    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 ThenAll(Func<PromisedT, IEnumerable<IPromise>> chain)
    Parameters
    Type Name Description
    System.Func<PromisedT, System.Collections.Generic.IEnumerable<IPromise>> chain
    Returns
    Type Description
    IPromise
    Implements
    IPromise<PromisedT>.ThenAll(Func<PromisedT, IEnumerable<IPromise>>)

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

    Chain an enumerable of promises, all of which must resolve. Returns a promise for a collection of the resolved results. 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<PromisedT, IEnumerable<IPromise<ConvertedT>>> chain)
    Parameters
    Type Name Description
    System.Func<PromisedT, System.Collections.Generic.IEnumerable<IPromise<ConvertedT>>> chain
    Returns
    Type Description
    IPromise<System.Collections.Generic.IEnumerable<ConvertedT>>
    Type Parameters
    Name Description
    ConvertedT
    Implements
    IPromise<PromisedT>.ThenAll<ConvertedT>(Func<PromisedT, IEnumerable<IPromise<ConvertedT>>>)

    ThenRace(Func<PromisedT, IEnumerable<IPromise>>)

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

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

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

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

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

    WithName(String)

    Set the name of the promise, useful for debugging.

    Declaration
    public IPromise<PromisedT> WithName(string name)
    Parameters
    Type Name Description
    System.String name
    Returns
    Type Description
    IPromise<PromisedT>
    Implements
    IPromise<PromisedT>.WithName(String)
    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