-
Notifications
You must be signed in to change notification settings - Fork 5
/
promise.lua
69 lines (56 loc) · 3.26 KB
/
promise.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
---@diagnostic disable: unused-local, missing-return
---@alias PromiseExecutor fun(resolve: fun(value: any), reject: fun(reason?: any))
---@class Promise
---@field loop PromiseAsyncEventLoop
---@overload fun(executor: PromiseExecutor): Promise
local Promise = {}
---Creates a new Promise.
---@param executor PromiseExecutor A callback used to initialize the promise. This callback is passed two arguments:
---a resolve callback used to resolve the promise with a value or the result of another promise,
---and a reject callback used to reject the promise with a provided reason or error.
---@return Promise promise A new Promise.
function Promise:new(executor) end
---Attaches callbacks for the resolution and/or rejection of the Promise.
---@param onFulfilled? fun(value: any): any The callback to execute when the Promise is resolved.
---@param onRejected? fun(reason: any): any The callback to execute when the Promise is rejected.
---@return Promise promise A Promise for the completion of which ever callback is executed.
function Promise:thenCall(onFulfilled, onRejected) end
---Attaches a callback for only the rejection of the Promise.
---@param onRejected? fun(reason: any): any The callback to execute when the Promise is rejected.
---@return Promise promise A Promise for the completion of the callback.
function Promise:catch(onRejected) end
---Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected).
---The resolved value cannot be modified from the callback.
---@param onFinally? fun() The callback to execute when the Promise is settled (fulfilled or rejected).
---@return Promise promise A new Promise.
function Promise:finally(onFinally) end
---Creates a new resolved promise for the provided value.
---@param value? any A value, or the promise passed as value
---@return Promise promise A resolved promise.
function Promise.resolve(value) end
---Creates a new rejected promise for the provided reason.
---@param reason? any The reason the Promise was rejected.
---@return Promise promise A new rejected Promise.
function Promise.reject(reason) end
---Creates a Promise that is resolved with a table of results when all of the provided
---Promises resolve, or rejected when any Promise is rejected.
---@param values table<any, Promise> A table of Promises.
---@return Promise promise A new Promise.
function Promise.all(values) end
---Creates a Promise that is resolved with a table of results when all of the provided
---Promises resolve or reject.
---@param values table<any, Promise> A table of Promises.
---@return Promise promise A new Promise.
function Promise.allSettled(values) end
---The any function returns a Promise that is fulfilled by the first given Promise to be fulfilled,
---or rejected with an AggregateError containing an table of rejection reasons if all of the
---given Promises are rejected. It resolves all elements of the passed table to Promises as it runs this algorithm.
---@param values table<any, Promise>
---@return Promise promise A new Promise
function Promise.any(values) end
---Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
---or rejected.
---@param values table<any, Promise> A table of Promises.
---@return Promise promise A new Promise.
function Promise.race(values) end
return Promise