Software /
code /
prosody
Comparison
util/promise.lua @ 12802:4a8740e01813
Merge 0.12->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 12 Dec 2022 07:10:54 +0100 |
parent | 12750:c882d2460bb1 |
child | 12975:d10957394a3c |
comparison
equal
deleted
inserted
replaced
12801:ebd6b4d8bf04 | 12802:4a8740e01813 |
---|---|
1 local promise_methods = {}; | 1 local promise_methods = {}; |
2 local promise_mt = { __name = "promise", __index = promise_methods }; | 2 local promise_mt = { __name = "promise", __index = promise_methods }; |
3 | 3 |
4 local xpcall = require "util.xpcall".xpcall; | 4 local xpcall = require "util.xpcall".xpcall; |
5 local unpack = table.unpack or unpack; --luacheck: ignore 113 | 5 local unpack = table.unpack; |
6 | 6 |
7 function promise_mt:__tostring() | 7 function promise_mt:__tostring() |
8 return "promise (" .. (self._state or "invalid") .. ")"; | 8 return "promise (" .. (self._state or "invalid") .. ")"; |
9 end | 9 end |
10 | 10 |
55 promise._pending_on_rejected = nil; | 55 promise._pending_on_rejected = nil; |
56 return true; | 56 return true; |
57 end | 57 end |
58 | 58 |
59 local function new_resolve_functions(p) | 59 local function new_resolve_functions(p) |
60 local resolved = false; | |
61 local function _resolve(v) | 60 local function _resolve(v) |
62 if resolved then return; end | |
63 resolved = true; | |
64 if is_promise(v) then | 61 if is_promise(v) then |
65 v:next(new_resolve_functions(p)); | 62 v:next(new_resolve_functions(p)); |
66 elseif promise_settle(p, "fulfilled", next_fulfilled, p._pending_on_fulfilled, v) then | 63 elseif promise_settle(p, "fulfilled", next_fulfilled, p._pending_on_fulfilled, v) then |
67 p.value = v; | 64 p.value = v; |
68 end | 65 end |
69 | 66 |
70 end | 67 end |
71 local function _reject(e) | 68 local function _reject(e) |
72 if resolved then return; end | |
73 resolved = true; | |
74 if promise_settle(p, "rejected", next_rejected, p._pending_on_rejected, e) then | 69 if promise_settle(p, "rejected", next_rejected, p._pending_on_rejected, e) then |
75 p.reason = e; | 70 p.reason = e; |
76 end | 71 end |
77 end | 72 end |
78 return _resolve, _reject; | 73 return _resolve, _reject; |