Software /
code /
prosody
Comparison
util/promise.lua @ 9513:4f4f9823bd1d
util.promise: Some code relocation
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 18 Oct 2018 12:12:52 +0100 |
parent | 9512:439cf3bbe5f3 |
child | 9514:9db707a86a25 |
comparison
equal
deleted
inserted
replaced
9512:439cf3bbe5f3 | 9513:4f4f9823bd1d |
---|---|
53 end | 53 end |
54 end | 54 end |
55 return _resolve, _reject; | 55 return _resolve, _reject; |
56 end | 56 end |
57 | 57 |
58 local function new(f) | |
59 local p = setmetatable({ _state = "pending", _next = next_pending, _pending_on_fulfilled = {}, _pending_on_rejected = {} }, promise_mt); | |
60 if f then | |
61 local resolve, reject = new_resolve_functions(p); | |
62 local ok, ret = pcall(f, resolve, reject); | |
63 if not ok and p._state == "pending" then | |
64 reject(ret); | |
65 end | |
66 end | |
67 return p; | |
68 end | |
69 | |
70 local function wrap_handler(f, resolve, reject) | 58 local function wrap_handler(f, resolve, reject) |
71 return function (param) | 59 return function (param) |
72 local ok, ret = pcall(f, param); | 60 local ok, ret = pcall(f, param); |
73 if ok then | 61 if ok then |
74 resolve(ret); | 62 resolve(ret); |
76 reject(ret); | 64 reject(ret); |
77 end | 65 end |
78 end; | 66 end; |
79 end | 67 end |
80 | 68 |
81 function promise_methods:next(on_fulfilled, on_rejected) | 69 local function new(f) |
82 return new(function (resolve, reject) | 70 local p = setmetatable({ _state = "pending", _next = next_pending, _pending_on_fulfilled = {}, _pending_on_rejected = {} }, promise_mt); |
83 self:_next( | 71 if f then |
84 on_fulfilled and wrap_handler(on_fulfilled, resolve, reject) or nil, | 72 local resolve, reject = new_resolve_functions(p); |
85 on_rejected and wrap_handler(on_rejected, resolve, reject) or nil | 73 local ok, ret = pcall(f, resolve, reject); |
86 ); | 74 if not ok and p._state == "pending" then |
87 end); | 75 reject(ret); |
88 end | 76 end |
89 | 77 end |
90 function promise_methods:catch(on_rejected) | 78 return p; |
91 return self:next(nil, on_rejected); | |
92 end | 79 end |
93 | 80 |
94 local function all(promises) | 81 local function all(promises) |
95 return new(function (resolve, reject) | 82 return new(function (resolve, reject) |
96 local count, total, results = 0, #promises, {}; | 83 local count, total, results = 0, #promises, {}; |
124 return new(function (_, _reject) | 111 return new(function (_, _reject) |
125 _reject(v); | 112 _reject(v); |
126 end); | 113 end); |
127 end | 114 end |
128 | 115 |
116 function promise_methods:next(on_fulfilled, on_rejected) | |
117 return new(function (resolve, reject) --luacheck: ignore 431/resolve 431/reject | |
118 self:_next( | |
119 on_fulfilled and wrap_handler(on_fulfilled, resolve, reject) or nil, | |
120 on_rejected and wrap_handler(on_rejected, resolve, reject) or nil | |
121 ); | |
122 end); | |
123 end | |
124 | |
125 function promise_methods:catch(on_rejected) | |
126 return self:next(nil, on_rejected); | |
127 end | |
128 | |
129 return { | 129 return { |
130 new = new; | 130 new = new; |
131 resolve = resolve; | 131 resolve = resolve; |
132 reject = reject; | 132 reject = reject; |
133 all = all; | 133 all = all; |