Skip to content

Commit 97faeeb

Browse files
puzpuzpuztargos
authored andcommitted
doc: improve ALS.enterWith and exit descriptions
PR-URL: #36705 Refs: #36683 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent ff212f4 commit 97faeeb

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

doc/api/async_hooks.md

+15-10
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ added: v13.10.0
990990
-->
991991

992992
Creates a new instance of `AsyncLocalStorage`. Store is only provided within a
993-
`run` method call.
993+
`run` or after `enterWith` method call.
994994

995995
### `asyncLocalStorage.disable()`
996996
<!-- YAML
@@ -999,7 +999,7 @@ added: v13.10.0
999999

10001000
This method disables the instance of `AsyncLocalStorage`. All subsequent calls
10011001
to `asyncLocalStorage.getStore()` will return `undefined` until
1002-
`asyncLocalStorage.run()` is called again.
1002+
`asyncLocalStorage.run()` or `asyncLocalStorage.enterWith()` is called again.
10031003

10041004
When calling `asyncLocalStorage.disable()`, all current contexts linked to the
10051005
instance will be exited.
@@ -1021,7 +1021,8 @@ added: v13.10.0
10211021

10221022
This method returns the current store.
10231023
If this method is called outside of an asynchronous context initialized by
1024-
calling `asyncLocalStorage.run`, it will return `undefined`.
1024+
calling `asyncLocalStorage.run()` or `asyncLocalStorage.enterWith()`, it will
1025+
return `undefined`.
10251026

10261027
### `asyncLocalStorage.enterWith(store)`
10271028
<!-- YAML
@@ -1030,14 +1031,15 @@ added: v13.11.0
10301031

10311032
* `store` {any}
10321033

1033-
Calling `asyncLocalStorage.enterWith(store)` will transition into the context
1034-
for the remainder of the current synchronous execution and will persist
1035-
through any following asynchronous calls.
1034+
This method transitions into the context for the remainder of the current
1035+
synchronous execution and then persists the store through any following
1036+
asynchronous calls.
10361037

10371038
Example:
10381039

10391040
```js
10401041
const store = { id: 1 };
1042+
// Replaces previous store with the given store object
10411043
asyncLocalStorage.enterWith(store);
10421044
asyncLocalStorage.getStore(); // Returns the store object
10431045
someAsyncOperation(() => {
@@ -1048,7 +1050,9 @@ someAsyncOperation(() => {
10481050
This transition will continue for the _entire_ synchronous execution.
10491051
This means that if, for example, the context is entered within an event
10501052
handler subsequent event handlers will also run within that context unless
1051-
specifically bound to another context with an `AsyncResource`.
1053+
specifically bound to another context with an `AsyncResource`. That is why
1054+
`run` should be preferred over `enterWith` unless there are strong reasons
1055+
to use the latter method.
10521056

10531057
```js
10541058
const store = { id: 1 };
@@ -1110,14 +1114,15 @@ added: v13.10.0
11101114

11111115
This methods runs a function synchronously outside of a context and return its
11121116
return value. The store is not accessible within the callback function or
1113-
the asynchronous operations created within the callback.
1117+
the asynchronous operations created within the callback, i.e. any `getStore`
1118+
call done within the callback function will always return `undefined`.
11141119

11151120
Optionally, arguments can be passed to the function. They will be passed to
11161121
the callback function.
11171122

11181123
If the callback function throws an error, it will be thrown by `exit` too.
1119-
The stacktrace will not be impacted by this call and
1120-
the context will be re-entered.
1124+
The stacktrace will not be impacted by this call and the context will be
1125+
re-entered.
11211126

11221127
Example:
11231128

0 commit comments

Comments
 (0)