-
-
Notifications
You must be signed in to change notification settings - Fork 88
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,55 @@ | ||
# 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: | ||
|
||
```js | ||
function callback(err, data) { | ||
console.log("Callback got called with:", err, data); | ||
throw new Error("My error"); | ||
} | ||
|
||
// note: passing `err.message` for demo purposes, normally you would pass `err` | ||
Promise.resolve() | ||
.then(() => callback(null, "data")) | ||
.catch(err => callback(err.message, null)); | ||
``` | ||
|
||
If you run this example, your output will look like the following: | ||
|
||
``` | ||
Callback got called with: null data | ||
Callback got called with: My error null | ||
``` | ||
|
||
**How to fix it?** | ||
|
||
Ensure that your callback invocations are wrapped by a deferred execution function such as: | ||
- [setImmediate()] or [process.nextTick()]: for Node.js. | ||
- [setTimeout()]: for Browsers and Node.js. | ||
|
||
```js | ||
// node.js | ||
Promise.resolve() | ||
.then(() => setImmediate(() => callback(null, "data"))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
.catch(err => setImmediate(() => callback(err.message, null))); | ||
|
||
// node.js and browsers | ||
Promise.resolve() | ||
.then(() => setTimeout(() => callback(null, "data"), 0)) | ||
.catch(err => setTimeout(() => callback(err.message, null), 0)); | ||
``` | ||
|
||
Your output will now look like the following: | ||
|
||
```js | ||
Callback got called with: null data | ||
``` | ||
|
||
Finally, if your callbacks have a Node.js signature (i.e. `callback(err, data)`), consider using [nodeify] for simplifying your code. | ||
|
||
[nodeify]: https://www.npmjs.com/package/nodeify | ||
[Promise.prototype.then()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then | ||
[Promise.prototype.catch()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch | ||
[setImmediate()]: https://nodejs.org/docs/latest-v14.x/api/timers.html#timers_setimmediate_callback_args | ||
[process.nextTick()]: https://nodejs.org/docs/latest-v14.x/api/process.html#process_process_nexttick_callback_args | ||
[setTimeout()]: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.