A Promise is an object designed for monitoring asynchronous operations and their eventual outcomes, whether they result in success or failure
A Promise can exist in one of the following states:
Once a Promise has resolved to either "Success" or "Failed," its state becomes sealed along with its associated data or error message, and it remains immutable.
When dealing with a function that returns a Promise object, you have the flexibility to either monitor or disregard its eventual outcome. If the success or failure of the asynchronous operation doesn't impact your code, you can simply ignore the Promise object.
If you need to capture the result of an asynchronous operation, you can engage with the Promise object using one of the following styles:
This style simplifies the code, but it is limited to use within coroutines.
Reminder: Coroutines are computer program execution lines that can be suspended and resumed.
All Firecast events run inside coroutines, and you can initiate a new one using the Async.execute function.
Example 1 (using a variable to hold the Promise object):
local promise = Dialogs.asyncOpenFile("Select a image", "image/*"); local selectedFiles = await(promise); showMessage(selectedFiles[1].name); |
Example 2 (without using a variable to hold the Promise object):
local selectedFiles = await(Dialogs.asyncOpenFile("Select a image", "image/*")); showMessage(selectedFiles[1].name); |
Example 3 (using the sugar "await" method that Promises objects have):
local selectedFiles = Dialogs.asyncOpenFile("Select a image", "image/*"):await(); showMessage(selectedFiles[1].name); |
Please refer to the await function documentation for more details.
This style works both inside and outside of coroutines but can make your code less readable if used excessively.
Invoke the "thenDo" and/or "onError" methods that all Promise objects have. The functionality of these methods is clarified below.
Example 1 (using a variable to hold the Promise object):
local promise = Dialogs.asyncOpenFile("Select a image", "image/*");
promise:thenDo( function(selectedFiles) -- the argument "selectedFiles" receives the success data of the promise showMessage(selectedFiles[1].name); end); |
Example 2 (without using a variable to hold the Promise object):
Dialogs.asyncOpenFile("Select a image", "image/*"):thenDo( function(selectedFiles) -- the argument "selectedFiles" receives the success data of the promise showMessage(selectedFiles[1].name); end); |
Método |
Descrição |
|
This method is employed to synchronize with the resolution of the Promise, allowing your code to pause until the asynchronous operation concludes. The promise:await() method is functionally equivalent to await(promise). For detailed information, please consult the documentation for the await function. |
This method is employed to synchronize with the resolution of the Promise, allowing your code to pause until the asynchronous operation concludes. The promise:pawait() method is functionally equivalent to pawait(promise) For detailed information, please consult the documentation for the pawait function. |
|
promise:thenDo(successCallback) |
Set a continuation callback function that will be called when the Promise resolves to success. Arguments:
Return:
Remarks:
Example:
|
promise:thenDo(successCallback, failureCallback) |
Set continuation callback functions that will be called when the Promise resolves to success or failure. Arguments:
Return:
Remarks:
Example:
|
|
Set a continuation callback function that will be called when the Promise resolves, whether to success or failure. Arguments:
Return:
Remarks:
|
Set a delegation that will resolve the provided PromiseResolution object with the same outcome (success or failure) as the original promise. Arguments:
Return:
Remarks:
|
|
promise:onError(failureCallback) |
Set a continuation callback function that will be called when the Promise resolves to failure. Arguments:
Return:
Remarks:
Example:
|
Created with the Personal Edition of HelpNDoc: Qt Help documentation made easy