PromiseBase (open)
The Promise object is as a placeholder for the value of a currently executing operation. It can be resolved with a value, or rejected with an exception. Only the first resolvement or rejection will be used, once resolved the promise keeps that value.
Within asynchronous functions, you can wait for the promise to be resolved with AWAIT promise
,
with returns the value the promise was resolved with, or throws the exception the promise was
rejected with.
Syntax
LOADLIB "wh::promise.whlib";
OBJECTTYPE PromiseBase
Constructor
- MACRO NEW(FUNCTION PTR func)
Creates a new promise object
Variables
- RECORD userdata
Userdata
Properties
- PROPERTY canceltoken
Cancel token (will be cancelled when @a Cancel is called)
- PROPERTY rejectoncancel
Whether the promise must immediately be resolved with the cancel exception when cancelled
Functions
- MACRO Cancel()
Cancels the task behind the promise. The task must be built to handle the cancellation for this operation to have effect.
In an asynchronous function, you can use
(AWAIT GetAsyncControl)
to get the AsyncControl object which exposes the canceltoken that this function controls. - OBJECT FUNCTION LinkToken(OBJECT canceltoken)
Make this promise follow the cancellation of another canceltoken
- OBJECT FUNCTION OnError(FUNCTION PTR onerror)
Schedule functions to run when this promise is rejected
- OBJECT FUNCTION Then(FUNCTION PTR then, FUNCTION PTR onerror)
Schedule functions to run when this promise is resolved or rejected