Skip to content

Commit 6ffec50

Browse files
puzpuzpuzcodebytere
authored andcommitted
doc: add troubleshooting guide for AsyncLocalStorage
PR-URL: #33248 Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent dab5c38 commit 6ffec50

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

doc/api/async_hooks.md

+16
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,21 @@ In this example, the store is only available in the callback function and the
10851085
functions called by `foo`. Outside of `run`, calling `getStore` will return
10861086
`undefined`.
10871087

1088+
### Troubleshooting
1089+
1090+
In most cases your application or library code should have no issues with
1091+
`AsyncLocalStorage`. But in rare cases you may face situations when the
1092+
current store is lost in one of asynchronous operations. Then you should
1093+
consider the following options.
1094+
1095+
If your code is callback-based, it is enough to promisify it with
1096+
[`util.promisify()`][], so it starts working with native promises.
1097+
1098+
If you need to keep using callback-based API, or your code assumes
1099+
a custom thenable implementation, you should use [`AsyncResource`][] class
1100+
to associate the asynchronous operation with the correct execution context.
1101+
1102+
[`AsyncResource`]: #async_hooks_class_asyncresource
10881103
[`after` callback]: #async_hooks_after_asyncid
10891104
[`before` callback]: #async_hooks_before_asyncid
10901105
[`destroy` callback]: #async_hooks_destroy_asyncid
@@ -1094,3 +1109,4 @@ functions called by `foo`. Outside of `run`, calling `getStore` will return
10941109
[PromiseHooks]: https://docs.google.com/document/d/1rda3yKGHimKIhg5YeoAmCOtyURgsbTH_qaYR79FELlk/edit
10951110
[`Worker`]: worker_threads.html#worker_threads_class_worker
10961111
[promise execution tracking]: #async_hooks_promise_execution_tracking
1112+
[`util.promisify()`]: util.html#util_util_promisify_original

0 commit comments

Comments
 (0)