Skip to content

Commit a67e164

Browse files
authored
add isPromiseLike (#222)
1 parent bcb7fb4 commit a67e164

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

boot.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const { debug } = require('./lib/debug')
2020
const { validatePlugin } = require('./lib/validate-plugin')
2121
const { isBundledOrTypescriptPlugin } = require('./lib/is-bundled-or-typescript-plugin')
2222
const { loadPlugin } = require('./lib/load-plugin')
23+
const { isPromiseLike } = require('./lib/is-promise-like')
2324

2425
function wrap (server, opts, instance) {
2526
const expose = opts.expose || {}
@@ -408,14 +409,14 @@ function callWithCbOrNextTick (func, cb) {
408409
if (func.length === 0) {
409410
this._error = err
410411
res = func()
411-
if (res && !res[kAvvio] && typeof res.then === 'function') {
412+
if (isPromiseLike(res) && !res[kAvvio]) {
412413
res.then(() => process.nextTick(cb), (e) => process.nextTick(cb, e))
413414
} else {
414415
process.nextTick(cb)
415416
}
416417
} else if (func.length === 1) {
417418
res = func(err)
418-
if (res && !res[kAvvio] && typeof res.then === 'function') {
419+
if (isPromiseLike(res) && !res[kAvvio]) {
419420
res.then(() => process.nextTick(cb), (e) => process.nextTick(cb, e))
420421
} else {
421422
process.nextTick(cb)

lib/is-promise-like.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
/**
4+
* @param {any} maybePromiseLike
5+
* @returns {maybePromiseLike is PromiseLike}
6+
*/
7+
function isPromiseLike (maybePromiseLike) {
8+
return (
9+
maybePromiseLike !== null &&
10+
typeof maybePromiseLike === 'object' &&
11+
typeof maybePromiseLike.then === 'function'
12+
)
13+
}
14+
15+
module.exports = {
16+
isPromiseLike
17+
}

lib/load-plugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const { isPromiseLike } = require('./is-promise-like')
4+
35
/**
46
* @callback LoadPluginCallback
57
* @param {Error} [err]
@@ -13,7 +15,7 @@
1315
* @param {LoadPluginCallback} callback
1416
*/
1517
function loadPlugin (instance, plugin, callback) {
16-
if (typeof plugin.func.then === 'function') {
18+
if (isPromiseLike(plugin.func)) {
1719
plugin.func.then((fn) => {
1820
if (typeof fn.default === 'function') {
1921
fn = fn.default

plugin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { loadPlugin } = require('./lib/load-plugin')
88
const { createPromise } = require('./lib/create-promise')
99
const { AVV_ERR_READY_TIMEOUT } = require('./lib/errors')
1010
const { getPluginName } = require('./lib/get-plugin-name')
11+
const { isPromiseLike } = require('./lib/is-promise-like')
1112

1213
function Plugin (parent, func, options, isAfter, timeout) {
1314
this.started = false
@@ -99,7 +100,7 @@ Plugin.prototype.exec = function (server, cb) {
99100
this.emit('start', this.server ? this.server.name : null, this.name, Date.now())
100101
const promise = func(this.server, this.opts, done)
101102

102-
if (promise && typeof promise.then === 'function') {
103+
if (isPromiseLike(promise)) {
103104
debug('exec: resolving promise', name)
104105

105106
promise.then(

test/lib/is-promise-like.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
const { isPromiseLike } = require('../../lib/is-promise-like')
5+
6+
test('isPromiseLike', (t) => {
7+
t.plan(9)
8+
9+
t.equal(isPromiseLike(1), false)
10+
t.equal(isPromiseLike('function'), false)
11+
t.equal(isPromiseLike({}), false)
12+
t.equal(isPromiseLike([]), false)
13+
t.equal(isPromiseLike(null), false)
14+
15+
t.equal(isPromiseLike(function () {}), false)
16+
t.equal(isPromiseLike(new Promise((resolve) => resolve)), true)
17+
t.equal(isPromiseLike(Promise.resolve()), true)
18+
19+
t.equal(isPromiseLike({ then: () => {} }), true)
20+
})

0 commit comments

Comments
 (0)