aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2018-10-18 12:12:52 +0100
committerMatthew Wild <mwild1@gmail.com>2018-10-18 12:12:52 +0100
commit894fe1b808b918eb041f7aca0fef7723f0f80f78 (patch)
treeb92a74e1e69bd938342386f5083e97b316142231 /util
parent7bcf751702cfc10ad5946c9ed54784c95653441f (diff)
downloadprosody-894fe1b808b918eb041f7aca0fef7723f0f80f78.tar.gz
prosody-894fe1b808b918eb041f7aca0fef7723f0f80f78.zip
util.promise: Some code relocation
Diffstat (limited to 'util')
-rw-r--r--util/promise.lua46
1 files changed, 23 insertions, 23 deletions
diff --git a/util/promise.lua b/util/promise.lua
index 2583baa2..c988ddc9 100644
--- a/util/promise.lua
+++ b/util/promise.lua
@@ -55,18 +55,6 @@ local function new_resolve_functions(p)
return _resolve, _reject;
end
-local function new(f)
- local p = setmetatable({ _state = "pending", _next = next_pending, _pending_on_fulfilled = {}, _pending_on_rejected = {} }, promise_mt);
- if f then
- local resolve, reject = new_resolve_functions(p);
- local ok, ret = pcall(f, resolve, reject);
- if not ok and p._state == "pending" then
- reject(ret);
- end
- end
- return p;
-end
-
local function wrap_handler(f, resolve, reject)
return function (param)
local ok, ret = pcall(f, param);
@@ -78,17 +66,16 @@ local function wrap_handler(f, resolve, reject)
end;
end
-function promise_methods:next(on_fulfilled, on_rejected)
- return new(function (resolve, reject)
- self:_next(
- on_fulfilled and wrap_handler(on_fulfilled, resolve, reject) or nil,
- on_rejected and wrap_handler(on_rejected, resolve, reject) or nil
- );
- end);
-end
-
-function promise_methods:catch(on_rejected)
- return self:next(nil, on_rejected);
+local function new(f)
+ local p = setmetatable({ _state = "pending", _next = next_pending, _pending_on_fulfilled = {}, _pending_on_rejected = {} }, promise_mt);
+ if f then
+ local resolve, reject = new_resolve_functions(p);
+ local ok, ret = pcall(f, resolve, reject);
+ if not ok and p._state == "pending" then
+ reject(ret);
+ end
+ end
+ return p;
end
local function all(promises)
@@ -126,6 +113,19 @@ local function reject(v)
end);
end
+function promise_methods:next(on_fulfilled, on_rejected)
+ return new(function (resolve, reject) --luacheck: ignore 431/resolve 431/reject
+ self:_next(
+ on_fulfilled and wrap_handler(on_fulfilled, resolve, reject) or nil,
+ on_rejected and wrap_handler(on_rejected, resolve, reject) or nil
+ );
+ end);
+end
+
+function promise_methods:catch(on_rejected)
+ return self:next(nil, on_rejected);
+end
+
return {
new = new;
resolve = resolve;