Class Promise
Object that encapsulates a value or an outlook of there might possibly be a value.
Essentially an object that can be assigned a callback.
Functions
-
Promise.fromCallback (f, ...)
-
Creates a promise that represents result from a function that utilizes a callback.
The function has to expect its callback as a last parameters after all of the parameters that have been used to call this function.
Parameters:
- f
func
Function that returns its result through a callback. The regular return values are ignored.
- ...
List of parameters to
f
without the last one.
Returns:
A promise representing the result of f
that is given to its callback.
Usage:
function doSomething(data)
...
end
f(a,b,c,d, doSomething)
local p = Promise.fromCallback(f, a,b,c,d) p:Then(doSomething) -- schedule the actual callback
-
Promise:New ([t])
-
Creates a promise.
Parameters:
- t
tab
Optional parameters.
(optional)
Returns:
Promise
The created promise.
Fields
-
Promise.fulfilled
-
An already fulfilled, but empty promise.
Methods
-
Promise:Chain (other)
-
Chains current promise to another one.
Parameters:
-
Promise:Fulfill (...)
-
Marks promise as fulfilled and fills it with values.
Remarks:
Each promise can be fulfilled only once.
Parameters:
- ...
values that should be stored in the promise
-
Promise:Then (f)
-
Schedules a callback that should be executed once the promise gets fullfilled.
The callback may be executed synchronously if the promise is already fulfilled.
Remarks:
Can also be invoked as a call to the promise, as in instead of p:Then(f)
, you may use p(f)
Parameters:
- f
func
The callback will be called with the value of the promise.