Comparison

spec/util_promise_spec.lua @ 9549:800c274928bf

util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
author Matthew Wild <mwild1@gmail.com>
date Thu, 25 Oct 2018 14:38:00 +0100
parent 9517:b1c6ede17592
child 9550:98de4c2e2627
comparison
equal deleted inserted replaced
9548:a83afc22e9d7 9549:800c274928bf
134 assert.spy(cb).was_called(0); 134 assert.spy(cb).was_called(0);
135 assert.spy(err_cb).was_called(1); 135 assert.spy(err_cb).was_called(1);
136 assert.spy(cb2).was_called(1); 136 assert.spy(cb2).was_called(1);
137 assert.spy(err_cb2).was_called(0); 137 assert.spy(err_cb2).was_called(0);
138 assert.equal("ok", result); 138 assert.equal("ok", result);
139 end);
140
141 it("propagates errors down the chain, even when some handlers are not provided", function ()
142 local r, result;
143 local test_error = {};
144 local p = promise.new(function (resolve, reject)
145 r = resolve;
146 end);
147 local cb = spy.new(function () end);
148 local err_cb = spy.new(function (e) result = e end);
149 local p2 = p:next(function () error(test_error) end);
150 local p3 = p2:next(cb)
151 p3:catch(err_cb);
152 assert.spy(cb).was_called(0);
153 assert.spy(err_cb).was_called(0);
154 r("oh doh");
155 assert.spy(cb).was_called(0);
156 assert.spy(err_cb).was_called(1);
157 assert.equal(test_error, result);
158 end);
159
160 it("propagates values down the chain, even when some handlers are not provided", function ()
161 local r;
162 local p = promise.new(function (resolve, reject)
163 r = resolve;
164 end);
165 local cb = spy.new(function () end);
166 local err_cb = spy.new(function () end);
167 local p2 = p:next(function (v) return v; end);
168 local p3 = p2:catch(err_cb)
169 p3:next(cb);
170 assert.spy(cb).was_called(0);
171 assert.spy(err_cb).was_called(0);
172 r(1337);
173 assert.spy(cb).was_called(1);
174 assert.spy(cb).was_called_with(1337);
175 assert.spy(err_cb).was_called(0);
139 end); 176 end);
140 177
141 describe("race()", function () 178 describe("race()", function ()
142 it("works with fulfilled promises", function () 179 it("works with fulfilled promises", function ()
143 local p1, p2 = promise.resolve("yep"), promise.resolve("nope"); 180 local p1, p2 = promise.resolve("yep"), promise.resolve("nope");