Software /
code /
prosody
Annotate
util/promise.lua @ 11407:8d6e013377fa 0.11
mod_http: Restore ip field for requests without proxies
8603011e51fe optimized out more than just the loop, leaving the .ip
field blank when the request wasn't from a proxy.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 27 Feb 2021 20:45:45 +0100 |
parent | 11210:75636bf13bf0 |
child | 11211:1151140fc757 |
rev | line source |
---|---|
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local promise_methods = {}; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local promise_mt = { __name = "promise", __index = promise_methods }; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
9562
acf74ad0b795
Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
Matthew Wild <mwild1@gmail.com>
parents:
9559
diff
changeset
|
4 local xpcall = require "util.xpcall".xpcall; |
acf74ad0b795
Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
Matthew Wild <mwild1@gmail.com>
parents:
9559
diff
changeset
|
5 |
9515
2571c65b972f
util.promise: Add a string representation
Kim Alvefur <zash@zash.se>
parents:
9514
diff
changeset
|
6 function promise_mt:__tostring() |
2571c65b972f
util.promise: Add a string representation
Kim Alvefur <zash@zash.se>
parents:
9514
diff
changeset
|
7 return "promise (" .. (self._state or "invalid") .. ")"; |
2571c65b972f
util.promise: Add a string representation
Kim Alvefur <zash@zash.se>
parents:
9514
diff
changeset
|
8 end |
2571c65b972f
util.promise: Add a string representation
Kim Alvefur <zash@zash.se>
parents:
9514
diff
changeset
|
9 |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local function is_promise(o) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local mt = getmetatable(o); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 return mt == promise_mt; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
9549
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
15 local function wrap_handler(f, resolve, reject, default) |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
16 if not f then |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
17 return default; |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
18 end |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
19 return function (param) |
9559
7c65e3f38e6e
util.promise: Switch from pcall to xpcall to get tracebacks on exceptions
Matthew Wild <mwild1@gmail.com>
parents:
9558
diff
changeset
|
20 local ok, ret = xpcall(f, debug.traceback, param); |
9549
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
21 if ok then |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
22 resolve(ret); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
23 else |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
24 reject(ret); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
25 end |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
26 return true; |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
27 end; |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
9549
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
30 local function next_pending(self, on_fulfilled, on_rejected, resolve, reject) |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
31 table.insert(self._pending_on_fulfilled, wrap_handler(on_fulfilled, resolve, reject, resolve)); |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
32 table.insert(self._pending_on_rejected, wrap_handler(on_rejected, resolve, reject, reject)); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 |
9549
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
35 local function next_fulfilled(promise, on_fulfilled, on_rejected, resolve, reject) -- luacheck: ignore 212/on_rejected |
9550
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
36 wrap_handler(on_fulfilled, resolve, reject, resolve)(promise.value); |
9549
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
37 end |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
38 |
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
39 local function next_rejected(promise, on_fulfilled, on_rejected, resolve, reject) -- luacheck: ignore 212/on_fulfilled |
9550
98de4c2e2627
util.promise: Fix missing parameters
Matthew Wild <mwild1@gmail.com>
parents:
9549
diff
changeset
|
40 wrap_handler(on_rejected, resolve, reject, reject)(promise.reason); |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 local function promise_settle(promise, new_state, new_next, cbs, value) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 if promise._state ~= "pending" then |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 return; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 promise._state = new_state; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 promise._next = new_next; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 for _, cb in ipairs(cbs) do |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 cb(value); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 return true; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 local function new_resolve_functions(p) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 local resolved = false; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 local function _resolve(v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 if resolved then return; end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 resolved = true; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 if is_promise(v) then |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 v:next(new_resolve_functions(p)); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 elseif promise_settle(p, "fulfilled", next_fulfilled, p._pending_on_fulfilled, v) then |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 p.value = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 local function _reject(e) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 if resolved then return; end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 resolved = true; |
9558
5fa73fbb047f
util.promise: Remove the non-standard ability to pass a promise to reject()
Matthew Wild <mwild1@gmail.com>
parents:
9550
diff
changeset
|
70 if promise_settle(p, "rejected", next_rejected, p._pending_on_rejected, e) then |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 p.reason = e; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 return _resolve, _reject; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 |
9513
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
77 local function new(f) |
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
78 local p = setmetatable({ _state = "pending", _next = next_pending, _pending_on_fulfilled = {}, _pending_on_rejected = {} }, promise_mt); |
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
79 if f then |
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
80 local resolve, reject = new_resolve_functions(p); |
11210
75636bf13bf0
util.promise: Use xpcall() for promise function to preserve tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
9562
diff
changeset
|
81 local ok, ret = xpcall(f, debug.traceback, resolve, reject); |
9513
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
82 if not ok and p._state == "pending" then |
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
83 reject(ret); |
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
84 end |
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
85 end |
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
86 return p; |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 local function all(promises) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 return new(function (resolve, reject) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 local count, total, results = 0, #promises, {}; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 for i = 1, total do |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 promises[i]:next(function (v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 results[i] = v; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 count = count + 1; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 if count == total then |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 resolve(results); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 end, reject); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 local function race(promises) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 return new(function (resolve, reject) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 for i = 1, #promises do |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 promises[i]:next(resolve, reject); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 local function resolve(v) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 return new(function (_resolve) |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 _resolve(v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 local function reject(v) |
9510
8ef46d09386a
util.promise: Fix promise.reject() to return a rejected promise, and fix buggy test for it
Matthew Wild <mwild1@gmail.com>
parents:
9456
diff
changeset
|
119 return new(function (_, _reject) |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 _reject(v); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 end); |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 end |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 |
9517
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9515
diff
changeset
|
124 local function try(f) |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9515
diff
changeset
|
125 return resolve():next(function () return f(); end); |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9515
diff
changeset
|
126 end |
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9515
diff
changeset
|
127 |
9513
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
128 function promise_methods:next(on_fulfilled, on_rejected) |
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
129 return new(function (resolve, reject) --luacheck: ignore 431/resolve 431/reject |
9549
800c274928bf
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Matthew Wild <mwild1@gmail.com>
parents:
9548
diff
changeset
|
130 self:_next(on_fulfilled, on_rejected, resolve, reject); |
9513
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
131 end); |
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
132 end |
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
133 |
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
134 function promise_methods:catch(on_rejected) |
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
135 return self:next(nil, on_rejected); |
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
136 end |
4f4f9823bd1d
util.promise: Some code relocation
Matthew Wild <mwild1@gmail.com>
parents:
9512
diff
changeset
|
137 |
9514
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9513
diff
changeset
|
138 function promise_methods:finally(on_finally) |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9513
diff
changeset
|
139 local function _on_finally(value) on_finally(); return value; end |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9513
diff
changeset
|
140 local function _on_catch_finally(err) on_finally(); return reject(err); end |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9513
diff
changeset
|
141 return self:next(_on_finally, _on_catch_finally); |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9513
diff
changeset
|
142 end |
9db707a86a25
util.promise: Add promise:finally()
Matthew Wild <mwild1@gmail.com>
parents:
9513
diff
changeset
|
143 |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 return { |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 new = new; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 resolve = resolve; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 reject = reject; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 all = all; |
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 race = race; |
9517
b1c6ede17592
util.promise: Add promise.try()
Matthew Wild <mwild1@gmail.com>
parents:
9515
diff
changeset
|
150 try = try; |
9548
a83afc22e9d7
util.promise: Export is_promise()
Matthew Wild <mwild1@gmail.com>
parents:
9546
diff
changeset
|
151 is_promise = is_promise; |
9456
d54a0e129af8
util.promise: ES6-like API for promises
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 } |