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 | e7a26ebc24cc8e22c84108ec8c6e94a1f1f1bc63 (patch) | |
tree | 1fa523ee426ab08664929087982e08ddc961d210 /spec | |
parent | ef2ab2370018831241e4e8a42b29e1d55ad9c16f (diff) | |
download | prosody-e7a26ebc24cc8e22c84108ec8c6e94a1f1f1bc63.tar.gz prosody-e7a26ebc24cc8e22c84108ec8c6e94a1f1f1bc63.zip |
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Diffstat (limited to 'spec')
-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"); |