summaryrefslogtreecommitdiff
path: root/doc/api/process.md
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2018-08-09 21:06:32 +0200
committerRuben Bridgewater <ruben@bridgewater.de>2018-09-25 10:04:09 +0200
commit5605cec0db9481d4cc85da1a7869f1d725c4357f (patch)
treeda5cc42b3db28614fde90b7686550830123c024d /doc/api/process.md
parentea3bb9add27cfddf8a8408fefa65f8dc75a5d0bb (diff)
downloadandroid-node-v8-5605cec0db9481d4cc85da1a7869f1d725c4357f.tar.gz
android-node-v8-5605cec0db9481d4cc85da1a7869f1d725c4357f.tar.bz2
android-node-v8-5605cec0db9481d4cc85da1a7869f1d725c4357f.zip
process: add `multipleResolves` event
This adds the `multipleResolves` event to track promises that resolve more than once or that reject after resolving. It is important to expose this to the user to make sure the application runs as expected. Without such warnings it would be very hard to debug these situations. PR-URL: https://github.com/nodejs/node/pull/22218 Fixes: https://github.com/nodejs/promises-debugging/issues/8 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Diffstat (limited to 'doc/api/process.md')
-rw-r--r--doc/api/process.md53
1 files changed, 53 insertions, 0 deletions
diff --git a/doc/api/process.md b/doc/api/process.md
index 714a9bddc5..07d9b43b55 100644
--- a/doc/api/process.md
+++ b/doc/api/process.md
@@ -97,6 +97,59 @@ the child process.
The message goes through serialization and parsing. The resulting message might
not be the same as what is originally sent.
+### Event: 'multipleResolves'
+<!-- YAML
+added: REPLACEME
+-->
+
+* `type` {string} The error type. One of `'resolve'` or `'reject'`.
+* `promise` {Promise} The promise that resolved or rejected more than once.
+* `value` {any} The value with which the promise was either resolved or
+ rejected after the original resolve.
+
+The `'multipleResolves'` event is emitted whenever a `Promise` has been either:
+
+* Resolved more than once.
+* Rejected more than once.
+* Rejected after resolve.
+* Resolved after reject.
+
+This is useful for tracking errors in your application while using the promise
+constructor. Otherwise such mistakes are silently swallowed due to being in a
+dead zone.
+
+It is recommended to end the process on such errors, since the process could be
+in an undefined state. While using the promise constructor make sure that it is
+guaranteed to trigger the `resolve()` or `reject()` functions exactly once per
+call and never call both functions in the same call.
+
+```js
+process.on('multipleResolves', (type, promise, reason) => {
+ console.error(type, promise, reason);
+ setImmediate(() => process.exit(1));
+});
+
+async function main() {
+ try {
+ return await new Promise((resolve, reject) => {
+ resolve('First call');
+ resolve('Swallowed resolve');
+ reject(new Error('Swallowed reject'));
+ });
+ } catch {
+ throw new Error('Failed');
+ }
+}
+
+main().then(console.log);
+// resolve: Promise { 'First call' } 'Swallowed resolve'
+// reject: Promise { 'First call' } Error: Swallowed reject
+// at Promise (*)
+// at new Promise (<anonymous>)
+// at main (*)
+// First call
+```
+
### Event: 'rejectionHandled'
<!-- YAML
added: v1.4.1