docs.unity3d.com
    Show / Hide Table of Contents

    Class Promise<T>

    A Promise is used for operations that retrieve data asynchronously. Use this object to determine the status of the operation (i.e., whether it has completed), and the resulting data.

    Inheritance
    Object
    CustomYieldInstruction
    Promise<T>
    Inherited Members
    UnityEngine.CustomYieldInstruction.MoveNext()
    UnityEngine.CustomYieldInstruction.Reset()
    UnityEngine.CustomYieldInstruction.Current
    Namespace: UnityEngine.XR.ARSubsystems
    Syntax
    public abstract class Promise<T> : CustomYieldInstruction, IEnumerator
    Type Parameters
    Name Description
    T

    The type of information the asynchronous operation retrieves.

    Remarks

    Since Promise<T> derives from CustomYieldInstruction, you can yield return on a Promise in a coroutine. If you prefer not to use the Promise as a coroutine, you may manually check keepWaiting to determine if the operation has completed. Once the operation is complete, you can get the resulting value from result.

    Examples

    Example usage in a coroutine:

    IEnumerator MyCoroutine()
    {
        var promise = GetDataAsync();
        yield return promise;
        Debug.LogFormat("Operation complete. Result = {0}", promise.result);
    }

    Properties

    keepWaiting

    Will return true as long as the operation has not yet completed.

    Declaration
    public override bool keepWaiting { get; }
    Property Value
    Type Description
    Boolean
    Overrides
    CustomYieldInstruction.keepWaiting

    result

    The result of the asynchronous operation. Not valid until keepWaiting returns false.

    Declaration
    public T result { get; }
    Property Value
    Type Description
    T

    Methods

    CreateResolvedPromise(T)

    Creates a resolved promise, i.e., one that is already complete.

    Declaration
    public static Promise<T> CreateResolvedPromise(T result)
    Parameters
    Type Name Description
    T result

    The result of the operation.

    Returns
    Type Description
    Promise<T>

    A completed Promise<T>.

    OnKeepWaiting()

    Invoked whenever keepWaiting is queried. Implement this to perform per-frame updates.

    Declaration
    protected abstract void OnKeepWaiting()

    Resolve(T)

    The creator of the Promise<T> should call this when the asynchronous operation completes.

    Declaration
    protected void Resolve(T result)
    Parameters
    Type Name Description
    T result

    The result of the asychronous operation.

    Back to top
    Terms of use
    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