Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add isPromiseLike #222

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const { debug } = require('./lib/debug')
const { validatePlugin } = require('./lib/validate-plugin')
const { isBundledOrTypescriptPlugin } = require('./lib/is-bundled-or-typescript-plugin')
const { loadPlugin } = require('./lib/load-plugin')
const { isPromiseLike } = require('./lib/is-promise-like')

function wrap (server, opts, instance) {
const expose = opts.expose || {}
Expand Down Expand Up @@ -408,14 +409,14 @@ function callWithCbOrNextTick (func, cb) {
if (func.length === 0) {
this._error = err
res = func()
if (res && !res[kAvvio] && typeof res.then === 'function') {
if (isPromiseLike(res) && !res[kAvvio]) {
res.then(() => process.nextTick(cb), (e) => process.nextTick(cb, e))
} else {
process.nextTick(cb)
}
} else if (func.length === 1) {
res = func(err)
if (res && !res[kAvvio] && typeof res.then === 'function') {
if (isPromiseLike(res) && !res[kAvvio]) {
res.then(() => process.nextTick(cb), (e) => process.nextTick(cb, e))
} else {
process.nextTick(cb)
Expand Down
17 changes: 17 additions & 0 deletions lib/is-promise-like.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

/**
* @param {any} maybePromiseLike
* @returns {maybePromiseLike is PromiseLike}
*/
function isPromiseLike (maybePromiseLike) {
return (
maybePromiseLike !== null &&
typeof maybePromiseLike === 'object' &&
typeof maybePromiseLike.then === 'function'
)
}

module.exports = {
isPromiseLike
}
4 changes: 3 additions & 1 deletion lib/load-plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const { isPromiseLike } = require('./is-promise-like')

/**
* @callback LoadPluginCallback
* @param {Error} [err]
Expand All @@ -13,7 +15,7 @@
* @param {LoadPluginCallback} callback
*/
function loadPlugin (instance, plugin, callback) {
if (typeof plugin.func.then === 'function') {
if (isPromiseLike(plugin.func)) {
plugin.func.then((fn) => {
if (typeof fn.default === 'function') {
fn = fn.default
Expand Down
3 changes: 2 additions & 1 deletion plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { loadPlugin } = require('./lib/load-plugin')
const { createPromise } = require('./lib/create-promise')
const { AVV_ERR_READY_TIMEOUT } = require('./lib/errors')
const { getPluginName } = require('./lib/get-plugin-name')
const { isPromiseLike } = require('./lib/is-promise-like')

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

if (promise && typeof promise.then === 'function') {
if (isPromiseLike(promise)) {
debug('exec: resolving promise', name)

promise.then(
Expand Down
20 changes: 20 additions & 0 deletions test/lib/is-promise-like.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const { test } = require('tap')
const { isPromiseLike } = require('../../lib/is-promise-like')

test('isPromiseLike', (t) => {
t.plan(9)

t.equal(isPromiseLike(1), false)
t.equal(isPromiseLike('function'), false)
t.equal(isPromiseLike({}), false)
t.equal(isPromiseLike([]), false)
t.equal(isPromiseLike(null), false)

t.equal(isPromiseLike(function () {}), false)
t.equal(isPromiseLike(new Promise((resolve) => resolve)), true)
t.equal(isPromiseLike(Promise.resolve()), true)

t.equal(isPromiseLike({ then: () => {} }), true)
})