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

Proper support for after with a passed async function in wrapped mode #75

Merged
merged 2 commits into from
Sep 4, 2018
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
22 changes: 19 additions & 3 deletions boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,27 @@ function encapsulateTwoParam (func, that) {
function encapsulateThreeParam (func, that) {
return _encapsulateThreeParam.bind(that)
function _encapsulateThreeParam (err, cb) {
var res
if (!func) {
process.nextTick(cb)
} else if (func.length === 0 || func.length === 1) {
func(err)
process.nextTick(cb)
} else if (func.length === 0) {
res = func()
if (res && res.then) {
res.then(function () {
process.nextTick(cb, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need process.nextTick?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to avoid any thrown value within the callback to be catched by the promiss.

}, cb)
} else {
process.nextTick(cb, err)
}
} else if (func.length === 1) {
res = func(err)
if (res && res.then) {
res.then(function () {
process.nextTick(cb)
}, cb)
} else {
process.nextTick(cb)
}
} else if (func.length === 2) {
func(err, cb)
} else {
Expand Down
28 changes: 28 additions & 0 deletions test/after-pass-through.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

const t = require('tap')
const boot = require('..')
const app = {}

boot(app)

t.plan(5)

app.use(function (f, opts) {
return Promise.reject(new Error('kaboom'))
}).after(function (err, cb) {
t.pass('this is just called')
cb(err)
}).after(function () {
t.pass('this is just called')
}).after(function (err, cb) {
t.pass('this is just called')
cb(err)
})

app.ready().then(() => {
t.fail('this should not be called')
}).catch(err => {
t.ok(err)
t.strictEqual(err.message, 'kaboom')
})
46 changes: 46 additions & 0 deletions test/async-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,49 @@ test('after', async (t) => {
t.ok(thirdLoaded, 'third is loaded')
t.pass('booted')
})

test('after wrapped', async (t) => {
t.plan(15)

const app = {}
boot(app)
let firstLoaded = false
let secondLoaded = false
let thirdLoaded = false

app.use(first)

async function first (s, opts) {
t.notOk(firstLoaded, 'first is not loaded')
t.notOk(secondLoaded, 'second is not loaded')
t.notOk(thirdLoaded, 'third is not loaded')
firstLoaded = true
s.after(second)
s.after(third)
}

async function second (err) {
t.error(err)
t.ok(firstLoaded, 'first is loaded')
t.notOk(secondLoaded, 'second is not loaded')
t.notOk(thirdLoaded, 'third is not loaded')
await sleep(10)
secondLoaded = true
}

async function third () {
t.ok(firstLoaded, 'first is loaded')
t.ok(secondLoaded, 'second is loaded')
t.notOk(thirdLoaded, 'third is not loaded')
await sleep(10)
thirdLoaded = true
}

const readyContext = await app.ready()

t.equal(app, readyContext)
t.ok(firstLoaded, 'first is loaded')
t.ok(secondLoaded, 'second is loaded')
t.ok(thirdLoaded, 'third is loaded')
t.pass('booted')
})