diff --git a/lib/index.js b/lib/index.js index 91d64fb..fb4d603 100644 --- a/lib/index.js +++ b/lib/index.js @@ -173,7 +173,7 @@ export function wrap(middleware, callback) { } if (!fnExpectsCallback) { - if (result instanceof Promise) { + if (result && result.then && typeof result.then === 'function') { result.then(then, done) } else if (result instanceof Error) { done(result) diff --git a/test.js b/test.js index d7f5a93..dbd9669 100644 --- a/test.js +++ b/test.js @@ -267,6 +267,48 @@ test('promise middleware', async function (t) { }) } ) + + await t.test('should support thenables', async function () { + // See: + let calls = 0 + + /** @type {Promise} */ + const promise = new Promise(function (resolve) { + trough() + .use(function () { + return { + /** + * @param {(value: unknown) => void} resolve + */ + // eslint-disable-next-line unicorn/no-thenable + then(resolve) { + setTimeout(function () { + resolve(42) + }) + } + } + }) + .run( + /** + * @param {unknown} [error] + * @param {unknown} [value] + * @returns {undefined} + */ + function (error, value) { + assert.equal(error, null) + assert.equal(value, 42) + calls++ + resolve(undefined) + } + ) + }) + + assert.equal(calls, 0) + + await promise + + assert.equal(calls, 1) + }) }) test('asynchronous middleware', async function (t) {