diff --git a/src/index.js b/src/index.js index 3567bf0b52e..d701c10003c 100644 --- a/src/index.js +++ b/src/index.js @@ -25,11 +25,10 @@ export default function maybeExtendPromise(Promise) { function handler(p, operation, ...args) { const h = promiseToHandler.get(p) || forwardingHandler; if (typeof h[operation] !== 'function') { - const handlerName = - h === forwardingHandler ? 'forwardingHandler' : 'unfulfilledHandler'; + const handlerName = h === forwardingHandler ? 'forwardingHandler' : 'unfulfilledHandler'; throw TypeError(`${handlerName}.${operation} is not a function`); } - return Promise.resolve(h[operation](p, ...args)); + return h[operation](p, ...args); } Object.defineProperties( @@ -180,13 +179,13 @@ export default function maybeExtendPromise(Promise) { // Just like platform Promises, multiple calls to resolve don't fail. if (!presenceToHandler.has(presence)) { + // Create table entries for the presence mapped to the fulfilledHandler. presenceToPromise.set(presence, handledP); presenceToHandler.set(presence, fulfilledHandler); } - // We need to invoke fulfilledHandler immediately, so add it - // to the mapping. - promiseToHandler.set(handledP, fulfilledHandler); + // Remove the mapping, as our fulfilledHandler should be used instead. + promiseToHandler.delete(handledP); // We committed to this presence, so resolve. handledResolve(presence); diff --git a/test/test.js b/test/test.js index 7994375b517..dd8b397452b 100644 --- a/test/test.js +++ b/test/test.js @@ -12,49 +12,6 @@ if (typeof window !== 'undefined') { }); } -test('immediate forwarding', async t => { - try { - const EPromise = maybeExtendPromise(Promise); - - const queue = []; - const handler = { - POST(_o, fn, args) { - queue.push([fn, args]); - return 'foo'; - }, - }; - let resolver; - const ep = EPromise.makeHandled(resolve => { - resolver = resolve; - }, handler); - - // Make sure asynchronous posts go through. - const expected = [['myfn', ['abc', 123]]]; - const firstPost = ep.post('myfn', ['abc', 123]).then(v => { - t.equal(v, 'foo', 'post return value is foo'); - t.deepEqual(queue, expected, 'single post in queue'); - }); - - t.deepEqual(queue, expected, 'unfulfilled post is synchronous'); - await firstPost; - - const target = {}; - resolver(target, handler); - expected.push(['myotherfn', ['def', 456]]); - const secondPost = ep.post('myotherfn', ['def', 456]).then(v => { - t.equal(v, 'foo', 'second post return value is foo'); - t.deepEqual(queue, expected, 'second post is queued'); - }); - - t.deepEqual(queue, expected, 'fulfilled post is synchronous'); - await secondPost; - } catch (e) { - t.assert(false, e); - } finally { - t.end(); - } -}); - test('maybeExtendPromise will not overwrite', async t => { try { const { makeHandled: secondMakeHandled } = maybeExtendPromise(Promise);