Software /
code /
prosody
Comparison
util/promise.lua @ 11947:073e53b72792
util.promise: Support delayed promise execution
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 05 Jan 2019 07:08:24 +0100 |
parent | 11486:78d843faaffc |
child | 12589:39ae08180c81 |
comparison
equal
deleted
inserted
replaced
11946:c0a01e5f5656 | 11947:073e53b72792 |
---|---|
76 end | 76 end |
77 end | 77 end |
78 return _resolve, _reject; | 78 return _resolve, _reject; |
79 end | 79 end |
80 | 80 |
81 local next_tick = function (f) | |
82 f(); | |
83 end | |
84 | |
81 local function new(f) | 85 local function new(f) |
82 local p = setmetatable({ _state = "pending", _next = next_pending, _pending_on_fulfilled = {}, _pending_on_rejected = {} }, promise_mt); | 86 local p = setmetatable({ _state = "pending", _next = next_pending, _pending_on_fulfilled = {}, _pending_on_rejected = {} }, promise_mt); |
83 if f then | 87 if f then |
84 local resolve, reject = new_resolve_functions(p); | 88 next_tick(function() |
85 local ok, ret = xpcall(f, debug.traceback, resolve, reject); | 89 local resolve, reject = new_resolve_functions(p); |
86 if not ok and p._state == "pending" then | 90 local ok, ret = xpcall(f, debug.traceback, resolve, reject); |
87 reject(ret); | 91 if not ok and p._state == "pending" then |
88 end | 92 reject(ret); |
93 end | |
94 end); | |
89 end | 95 end |
90 return p; | 96 return p; |
91 end | 97 end |
92 | 98 |
93 local function all(promises) | 99 local function all(promises) |
201 all = all; | 207 all = all; |
202 all_settled = all_settled; | 208 all_settled = all_settled; |
203 race = race; | 209 race = race; |
204 try = try; | 210 try = try; |
205 is_promise = is_promise; | 211 is_promise = is_promise; |
212 set_nexttick = function(new_next_tick) next_tick = new_next_tick; end; | |
206 } | 213 } |