diff --git a/README.md b/README.md index a17c66a..ba458be 100644 --- a/README.md +++ b/README.md @@ -151,8 +151,6 @@ Loads one or more functions asynchronously. The function **must** have the signature: `instance, options, done` -However, if the function returns a `Promise` (i.e. `async`), the above function signature is not required. - Plugin example: ```js function plugin (server, opts, done) { @@ -161,8 +159,11 @@ function plugin (server, opts, done) { app.use(plugin) ``` -`done` should be called only once, when your plugin is ready to go. Additional -calls to `done` are ignored. +`done` should be called only once, when your plugin is ready to go. Additional calls to `done` are ignored. + +If your plugin is ready to go immediately after the function is evaluated, you can omit `done` from the signature. + +If the function returns a `Promise` (i.e. `async`), the above function signature is not required. `use` returns a thenable wrapped instance on which `use` is called, to support a chainable API that can also be awaited. diff --git a/lib/plugin.js b/lib/plugin.js index 8c62d87..a3f248d 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -130,6 +130,8 @@ Plugin.prototype.exec = function (server, callback) { maybePromiseLike.then( () => process.nextTick(done), (e) => process.nextTick(done, e)) + } else if (func.length < 3) { + done() } } diff --git a/test/load-plugin.test.js b/test/load-plugin.test.js index 722791c..e1400ed 100644 --- a/test/load-plugin.test.js +++ b/test/load-plugin.test.js @@ -31,6 +31,17 @@ test('catch an error when loading a plugin with sync function', (t) => { }) }) +test('successfully load a plugin with sync function without done as a parameter', (t) => { + t.plan(1) + const app = boot({}) + + const plugin = new Plugin(fastq(app, app._loadPluginNextTick, 1), function (instance, opts) { }, false, 0) + + app._loadPlugin(plugin, function (err) { + t.equal(err, undefined) + }) +}) + test('successfully load a plugin with async function', (t) => { t.plan(1) const app = boot({}) diff --git a/test/no-done.test.js b/test/no-done.test.js new file mode 100644 index 0000000..0270545 --- /dev/null +++ b/test/no-done.test.js @@ -0,0 +1,18 @@ +'use strict' + +const { test } = require('tap') +const boot = require('..') + +test('not taking done does not throw error.', (t) => { + t.plan(2) + + const app = boot() + + app.use(noDone).ready((err) => { + t.notOk(err, 'no error') + }) + + function noDone (s, opts) { + t.pass('did not throw') + } +})