diff options
author | Andrey Pechkurov <apechkurov@gmail.com> | 2020-05-05 16:46:25 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2020-05-08 02:00:24 +0300 |
commit | fedb0f4d54e8e68d39f488969d074773f7f95ffb (patch) | |
tree | 8d4ad4a0ccda0da331f57b7c9ec8e1fde3e2d1cd /doc/api/async_hooks.md | |
parent | dab51dddc8b928a67ad35e93ab2b8f6458fdb8c1 (diff) |
doc: add troubleshooting guide for AsyncLocalStorage
PR-URL: https://github.com/nodejs/node/pull/33248
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Diffstat (limited to 'doc/api/async_hooks.md')
-rw-r--r-- | doc/api/async_hooks.md | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/doc/api/async_hooks.md b/doc/api/async_hooks.md index 414165e61b2..698c8f25277 100644 --- a/doc/api/async_hooks.md +++ b/doc/api/async_hooks.md @@ -1085,6 +1085,21 @@ In this example, the store is only available in the callback function and the functions called by `foo`. Outside of `run`, calling `getStore` will return `undefined`. +### Troubleshooting + +In most cases your application or library code should have no issues with +`AsyncLocalStorage`. But in rare cases you may face situations when the +current store is lost in one of asynchronous operations. Then you should +consider the following options. + +If your code is callback-based, it is enough to promisify it with +[`util.promisify()`][], so it starts working with native promises. + +If you need to keep using callback-based API, or your code assumes +a custom thenable implementation, you should use [`AsyncResource`][] class +to associate the asynchronous operation with the correct execution context. + +[`AsyncResource`]: #async_hooks_class_asyncresource [`after` callback]: #async_hooks_after_asyncid [`before` callback]: #async_hooks_before_asyncid [`destroy` callback]: #async_hooks_destroy_asyncid @@ -1094,3 +1109,4 @@ functions called by `foo`. Outside of `run`, calling `getStore` will return [PromiseHooks]: https://docs.google.com/document/d/1rda3yKGHimKIhg5YeoAmCOtyURgsbTH_qaYR79FELlk/edit [`Worker`]: worker_threads.html#worker_threads_class_worker [promise execution tracking]: #async_hooks_promise_execution_tracking +[`util.promisify()`]: util.html#util_util_promisify_original |