Stay organized with collections
Save and categorize content based on your preferences.
InterruptiblePromise< T >
Promises represent the eventual completion of an asynchronous operation.
A promise has one of three states, PromiseState
, which can be obtained with InterruptiblePromise.State
:
PromiseState.Pending
- The operation is still pending. The result of the operation isn't available yet.
PromiseState.Done
- The operation is complete, and a result is available.
PromiseState.Cancelled
- The operation has been cancelled.
An InterruptiblePromise
starts in the PromiseState.Pending
state and transitions to PromiseState.Done
upon completion. If the Promise is cancelled using InterruptiblePromise.Cancel()
, then its state may become PromiseState.Cancelled
(see cancelling a Promise for caveats).
Obtaining results from a Promise
There are two ways of obtaining results from an InterruptiblePromise
:
Polling a Promise
When the InterruptiblePromise
is created, its PromiseState
is set to PromiseState.Pending
. You may poll the future using InterruptiblePromise.State
to query the state of the asynchronous operation. When its state is PromiseState.Done
, you may obtain the operation's result using InterruptiblePromise.Result
.
Use a Unity Coroutine
Promises use a
CustomYieldInstruction
to facilitate Unity coroutines. Use yield return promiseInstance
to pause execution of your coroutine. Unity will resume execution of your coroutine when InterruptiblePromise.State
is no longer PromiseState.Pending
.
public void CreatePromise()
{
ResolveAnchorOnRooftopPromise rooftopPromise =
AnchorManager.ResolveAnchorOnRooftopAsync(...);
StartCoroutine(CheckRooftopPromise(rooftopPromise));
}
private IEnumerator CheckRooftopPromise(ResolveAnchorOnTerrainPromise promise)
{
yield return promise;
if (promise.State == PromiseState.Cancelled) yield break;
var result = promise.Result;
/// Use the result of your promise here.
}
Cancelling a Promise
You can try to cancel an InterruptiblePromise
by calling InterruptiblePromise.Cancel()
. Due to multi-threading, it is possible that the cancel operation is not successful, and any Unity coroutine may successfully resume execution regardless.
Details |
Template Parameters |
T
|
The type of the async task result.
|
|
Summary
Inheritance
Inherits from:
UnityEngine::CustomYieldInstruction
Properties
|
Result
|
T
Gets the result, if the operation is done.
|
State
|
|
Public functions
|
Cancel()
|
void
Cancels execution of this promise if it's still pending.
|
Properties
Result
T Result
Gets the result, if the operation is done.
State
PromiseState State
Gets the PromiseState
associated with this promise.
Used to determine if the promise is still waiting for the result.
Public functions
Cancel
void Cancel()
Cancels execution of this promise if it's still pending.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-07-14 UTC.
[null,null,["Last updated 2025-07-14 UTC."],[],[],null,["# InterruptiblePromise< T > Class Reference\n\nInterruptiblePromise\\\u003c T \\\u003e\n===========================\n\nPromises represent the eventual completion of an asynchronous operation.\n\nA promise has one of three states, [PromiseState](/ar/reference/unity-arf/namespace/Google/XR/ARCoreExtensions#promisestate), which can be obtained with [InterruptiblePromise.State](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/Internal/InterruptiblePromise-T-#state):\n\n\n- `PromiseState.Pending` - The operation is still pending. The result of the operation isn't available yet.\n- `PromiseState.Done` - The operation is complete, and a result is available.\n- `PromiseState.Cancelled` - The operation has been cancelled.\n\n\u003cbr /\u003e\n\nAn `InterruptiblePromise` starts in the `PromiseState.Pending` state and transitions to `PromiseState.Done` upon completion. If the Promise is cancelled using [InterruptiblePromise.Cancel()](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/Internal/InterruptiblePromise-T-#cancel), then its state may become `PromiseState.Cancelled` (see [cancelling a Promise](#cancelling-a-promise) for caveats).\n\n\n### Obtaining results from a Promise\n\n\u003cbr /\u003e\n\nThere are two ways of obtaining results from an `InterruptiblePromise`:\n\n\n#### Polling a Promise\n\n\u003cbr /\u003e\n\nWhen the [InterruptiblePromise](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/Internal/InterruptiblePromise-T-#classGoogle_1_1XR_1_1ARCoreExtensions_1_1Internal_1_1InterruptiblePromise_3_01T_01_4) is created, its [PromiseState](/ar/reference/unity-arf/namespace/Google/XR/ARCoreExtensions#promisestate) is set to `PromiseState.Pending`. You may poll the future using [InterruptiblePromise.State](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/Internal/InterruptiblePromise-T-#state) to query the state of the asynchronous operation. When its state is `PromiseState.Done`, you may obtain the operation's result using [InterruptiblePromise.Result](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/Internal/InterruptiblePromise-T-#result).\n\n\n#### Use a Unity Coroutine\n\n\u003cbr /\u003e\n\nPromises use a [`CustomYieldInstruction`](https://docs.unity3d.com/ScriptReference/CustomYieldInstruction.html) to facilitate [Unity coroutines](https://docs.unity3d.com/Manual/Coroutines.html). Use `yield return `**promiseInstance** to pause execution of your coroutine. Unity will resume execution of your coroutine when [InterruptiblePromise.State](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/Internal/InterruptiblePromise-T-#state) is no longer `PromiseState.Pending`.\n\n\n\n public void CreatePromise()\n {\n /ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/ResolveAnchorOnRooftopPromise#classGoogle_1_1XR_1_1ARCoreExtensions_1_1ResolveAnchorOnRooftopPromise rooftopPromise =\n AnchorManager.ResolveAnchorOnRooftopAsync(...);\n StartCoroutine(CheckRooftopPromise(rooftopPromise));\n }\n private IEnumerator CheckRooftopPromise(ResolveAnchorOnTerrainPromise promise)\n {\n yield return promise;\n if (promise.State == PromiseState.Cancelled) yield break;\n var result = promise.Result;\n /// Use the result of your promise here.\n }\n\n\u003cbr /\u003e\n\n\n### Cancelling a Promise\n\n\u003cbr /\u003e\n\nYou can try to cancel an `InterruptiblePromise` by calling [InterruptiblePromise.Cancel()](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/Internal/InterruptiblePromise-T-#cancel). Due to multi-threading, it is possible that the cancel operation is not successful, and any [Unity coroutine](#use-a-unity-coroutine) may successfully resume execution regardless.\n\n\u003cbr /\u003e\n\n| Details ||\n|---------------------|-------------------------------------------------------------------------------------------|\n| Template Parameters | |-----|------------------------------------| | `T` | The type of the async task result. | |\n\nSummary\n-------\n\n### Inheritance\n\nInherits from: [`UnityEngine::CustomYieldInstruction`](https://docs.unity3d.com/ScriptReference/CustomYieldInstruction.html)\n\n| ### Properties ||\n|-------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Result](#result) | `T` Gets the result, if the operation is done. |\n| [State](#state) | [PromiseState](/ar/reference/unity-arf/namespace/Google/XR/ARCoreExtensions#promisestate) Gets the [PromiseState](/ar/reference/unity-arf/namespace/Google/XR/ARCoreExtensions#promisestate) associated with this promise. |\n\n| ### Public functions ||\n|-----------------------|-----------------------------------------------------------------|\n| [Cancel](#cancel)`()` | `void` Cancels execution of this promise if it's still pending. |\n\nProperties\n----------\n\n### Result\n\n```c#\nT Result\n``` \nGets the result, if the operation is done. \n\n### State\n\n```c#\nPromiseState State\n``` \nGets the [PromiseState](/ar/reference/unity-arf/namespace/Google/XR/ARCoreExtensions#promisestate) associated with this promise.\n\nUsed to determine if the promise is still waiting for the result.\n\nPublic functions\n----------------\n\n### Cancel\n\n```c#\nvoid Cancel()\n``` \nCancels execution of this promise if it's still pending."]]