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
Added a proper catch block for handling errors #385
Conversation
the try catch will save some promise allocation. However there is a risk that the catch part in the try/catch throw, resulting in an unhandled rejection. This code is fine, it's just for tests. |
Thanks for the explanation. Could we stick into a practise and merge it into the master branch? |
test/connect.js
Outdated
}) | ||
} catch (error) { | ||
await Promise.all([msg(s, 100, packet1), msg(s, 200, packet2)]) | ||
setImmediate(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would make this a promise and use await.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function finish () {
return new Promise((resolve, reject) => {
broker.close()
resolve(t.end)
})
}
(async () => {
await Promise.all([msg(s, 100, packet1), msg(s, 200, packet2)])
await finish()
})().catch(
(error) => {
t.fail(error)
}
)
Is the above what you mean? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or should it be like this?
(async () => {
await Promise.all([msg(s, 100, packet1), msg(s, 200, packet2)])
})().catch(
(error) => {
t.fail(error)
}
).finally(finish)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean
const immediate = promisify(setImmediate)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome!. I haven't seen we could promisify setImmediate
.
Like this?
const immediate = util.promisify(setImmediate)
;(async () => {
await Promise.all([msg(s, 100, packet1), msg(s, 200, packet2)])
await immediate().then(() => {
broker.close()
t.end()
})
})().catch(
(error) => {
t.fail(error)
}
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would make this a promise and use await.
May we know what the differences are?
test/connect.js
Outdated
await Promise.all([msg(s, 100, packet1), msg(s, 200, packet2)]) | ||
setImmediate(() => { | ||
await immediate().then(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
await immediate().then(() => { | |
await immediate() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome. You are a great teacher!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Pull Request Test Coverage Report for Build 16556a6f9c59eee72db902f6900eaad9d2fdd6d1-PR-385
💛 - Coveralls |
@mcollina Is it better to add .catch() block rather than try/catch?
any differences?