-
-
Notifications
You must be signed in to change notification settings - Fork 93
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
Update no-callback-in-promise.md #215
Conversation
Add documentation of why callbacks shouldn't be invoked inside `Promise`'s `then()` and `catch()` methods.
# Avoid calling `cb()` inside of a `then()` (use [nodeify][] instead) (no-callback-in-promise) | ||
# Avoid calling `cb()` inside of a `then()` or `catch()` (no-callback-in-promise) | ||
|
||
As a general rule, callbacks should never be directly invoked inside a [Promise.prototype.then()] or [Promise.prototype.catch()] method. That's because your callback may be unintentionally be invoked twice. Take the following example: |
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.
So this is a great explanation of a bug that can occur when mixing these two types of patterns. My purpose for introducing this rule was more to encourage teams to pick a common pattern such as callbacks, async/await or promise and stick with it for consistencies sake, but this is a decent explanation and honestly pretty well thought out.
```js | ||
// node.js | ||
Promise.resolve() | ||
.then(() => setImmediate(() => callback(null, "data"))) |
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.
while this works, i don't love these either, and would prefer that folks promisify
'd their code instead of doing this
I'm pretty ok with this merging as-is, but would appreciate any thoughts on if we should even encourage the |
Hello @xjamundx. Thanks for the review and the comments! I was really surprised when I triggered this rule for the first time and I had to search online what was wrong with mixing callbacks and promises. I read the source code of Both this original documentation and the article refer to using a library to fix the problem. However for curious people like me that wanted to understand what was wrong in the first place and how the library fixed it this was not enough. I wanted a more detailed explanation which motivated me to write this PR. While I agree that folks should avoid mixing both programming styles sometimes this is unavoidable. Take for example the AWS.Credentials.prototype.refresh() method. Creating a subclass of Is there a simpler way of implementing this method if I want to stick with I'm happy to update the guidance with something simpler if available. |
Hey @xjamundx any update on this? |
@marcogrcr let me run some tests tonight to make sure your recommendations don't cause any lint warnings |
With the current implementation these all fail:
But I'll just mention that in the docs and we can file a separate bug to support those cases. |
Published with a few changes in |
Add documentation of why callbacks shouldn't be invoked inside
Promise
'sthen()
andcatch()
methods.What is the purpose of this pull request?
What changes did you make? (Give an overview)
Added documentation to provide more context to people that want to understand the purpose of this rule.