util.promise: Fix missing parameters

This commit is contained in:
Matthew Wild 2018-10-25 15:24:52 +01:00
parent b74a643e34
commit e6ff6e333c
2 changed files with 30 additions and 4 deletions

View file

@ -142,7 +142,7 @@ describe("util.promise", function ()
local r, result; local r, result;
local test_error = {}; local test_error = {};
local p = promise.new(function (resolve, reject) local p = promise.new(function (resolve, reject)
r = resolve; r = reject;
end); end);
local cb = spy.new(function () end); local cb = spy.new(function () end);
local err_cb = spy.new(function (e) result = e end); local err_cb = spy.new(function (e) result = e end);
@ -154,7 +154,8 @@ describe("util.promise", function ()
r("oh doh"); r("oh doh");
assert.spy(cb).was_called(0); assert.spy(cb).was_called(0);
assert.spy(err_cb).was_called(1); assert.spy(err_cb).was_called(1);
assert.equal(test_error, result); assert.spy(err_cb).was_called_with("oh doh");
assert.equal("oh doh", result);
end); end);
it("propagates values down the chain, even when some handlers are not provided", function () it("propagates values down the chain, even when some handlers are not provided", function ()
@ -175,6 +176,31 @@ describe("util.promise", function ()
assert.spy(err_cb).was_called(0); assert.spy(err_cb).was_called(0);
end); end);
it("fulfilled promises do not call error handlers and do propagate value", function ()
local p = promise.resolve("foo");
local cb = spy.new(function () end);
local p2 = p:catch(cb);
assert.spy(cb).was_called(0);
local cb2 = spy.new(function () end);
local p3 = p2:catch(cb2);
assert.spy(cb2).was_called(0);
end);
it("rejected promises do not call fulfilled handlers and do propagate reason", function ()
local p = promise.reject("foo");
local cb = spy.new(function () end);
local p2 = p:next(cb);
assert.spy(cb).was_called(0);
local cb2 = spy.new(function () end);
local cb2_err = spy.new(function () end);
local p3 = p2:next(cb2, cb2_err);
assert.spy(cb2).was_called(0);
assert.spy(cb2_err).was_called(1);
assert.spy(cb2_err).was_called_with("foo");
end);
describe("race()", function () describe("race()", function ()
it("works with fulfilled promises", function () it("works with fulfilled promises", function ()
local p1, p2 = promise.resolve("yep"), promise.resolve("nope"); local p1, p2 = promise.resolve("yep"), promise.resolve("nope");

View file

@ -31,11 +31,11 @@ local function next_pending(self, on_fulfilled, on_rejected, resolve, reject)
end end
local function next_fulfilled(promise, on_fulfilled, on_rejected, resolve, reject) -- luacheck: ignore 212/on_rejected local function next_fulfilled(promise, on_fulfilled, on_rejected, resolve, reject) -- luacheck: ignore 212/on_rejected
wrap_handler(on_fulfilled, resolve, reject)(promise.value); wrap_handler(on_fulfilled, resolve, reject, resolve)(promise.value);
end end
local function next_rejected(promise, on_fulfilled, on_rejected, resolve, reject) -- luacheck: ignore 212/on_fulfilled local function next_rejected(promise, on_fulfilled, on_rejected, resolve, reject) -- luacheck: ignore 212/on_fulfilled
wrap_handler(on_rejected, resolve, reject)(promise.reason); wrap_handler(on_rejected, resolve, reject, reject)(promise.reason);
end end
local function promise_settle(promise, new_state, new_next, cbs, value) local function promise_settle(promise, new_state, new_next, cbs, value)