Comparison

util/promise.lua @ 9517:b1c6ede17592

util.promise: Add promise.try()
author Matthew Wild <mwild1@gmail.com>
date Thu, 18 Oct 2018 18:03:31 +0100
parent 9515:2571c65b972f
child 9546:c93496785022
comparison
equal deleted inserted replaced
9516:038446c50630 9517:b1c6ede17592
49 end 49 end
50 local function _reject(e) 50 local function _reject(e)
51 if resolved then return; end 51 if resolved then return; end
52 resolved = true; 52 resolved = true;
53 if is_promise(e) then 53 if is_promise(e) then
54 print ("WOAH") assert(false)
54 e:next(new_resolve_functions(p)); 55 e:next(new_resolve_functions(p));
55 elseif promise_settle(p, "rejected", next_rejected, p._pending_on_rejected, e) then 56 elseif promise_settle(p, "rejected", next_rejected, p._pending_on_rejected, e) then
56 p.reason = e; 57 p.reason = e;
57 end 58 end
58 end 59 end
115 return new(function (_, _reject) 116 return new(function (_, _reject)
116 _reject(v); 117 _reject(v);
117 end); 118 end);
118 end 119 end
119 120
121 local function try(f)
122 return resolve():next(function () return f(); end);
123 end
124
120 function promise_methods:next(on_fulfilled, on_rejected) 125 function promise_methods:next(on_fulfilled, on_rejected)
121 return new(function (resolve, reject) --luacheck: ignore 431/resolve 431/reject 126 return new(function (resolve, reject) --luacheck: ignore 431/resolve 431/reject
122 self:_next( 127 self:_next(
123 on_fulfilled and wrap_handler(on_fulfilled, resolve, reject) or nil, 128 on_fulfilled and wrap_handler(on_fulfilled, resolve, reject) or nil,
124 on_rejected and wrap_handler(on_rejected, resolve, reject) or nil 129 on_rejected and wrap_handler(on_rejected, resolve, reject) or nil
140 new = new; 145 new = new;
141 resolve = resolve; 146 resolve = resolve;
142 reject = reject; 147 reject = reject;
143 all = all; 148 all = all;
144 race = race; 149 race = race;
150 try = try;
145 } 151 }