diff options
author | Matthew Wild <mwild1@gmail.com> | 2018-10-25 14:38:00 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2018-10-25 14:38:00 +0100 |
commit | d6b2e9efbfcbff00a61f8fee075f2498e1d3575c (patch) | |
tree | 1fa523ee426ab08664929087982e08ddc961d210 /spec/util_promise_spec.lua | |
parent | df8f2fd6c58ea7042d701701f364dd864832dc4a (diff) | |
download | prosody-d6b2e9efbfcbff00a61f8fee075f2498e1d3575c.tar.gz prosody-d6b2e9efbfcbff00a61f8fee075f2498e1d3575c.zip |
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Diffstat (limited to 'spec/util_promise_spec.lua')
-rw-r--r-- | spec/util_promise_spec.lua | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/spec/util_promise_spec.lua b/spec/util_promise_spec.lua index c2a8ba95..a06232b1 100644 --- a/spec/util_promise_spec.lua +++ b/spec/util_promise_spec.lua @@ -138,6 +138,43 @@ describe("util.promise", function () assert.equal("ok", result); end); + it("propagates errors down the chain, even when some handlers are not provided", function () + local r, result; + local test_error = {}; + local p = promise.new(function (resolve, reject) + r = resolve; + end); + local cb = spy.new(function () end); + local err_cb = spy.new(function (e) result = e end); + local p2 = p:next(function () error(test_error) end); + local p3 = p2:next(cb) + p3:catch(err_cb); + assert.spy(cb).was_called(0); + assert.spy(err_cb).was_called(0); + r("oh doh"); + assert.spy(cb).was_called(0); + assert.spy(err_cb).was_called(1); + assert.equal(test_error, result); + end); + + it("propagates values down the chain, even when some handlers are not provided", function () + local r; + local p = promise.new(function (resolve, reject) + r = resolve; + end); + local cb = spy.new(function () end); + local err_cb = spy.new(function () end); + local p2 = p:next(function (v) return v; end); + local p3 = p2:catch(err_cb) + p3:next(cb); + assert.spy(cb).was_called(0); + assert.spy(err_cb).was_called(0); + r(1337); + assert.spy(cb).was_called(1); + assert.spy(cb).was_called_with(1337); + assert.spy(err_cb).was_called(0); + end); + describe("race()", function () it("works with fulfilled promises", function () local p1, p2 = promise.resolve("yep"), promise.resolve("nope"); |