asyncLocalStorage.exit(callback[, ...args])
稳定性: 1 - 实验性
callback<Function>...args<any>
在上下文之外同步运行一个函数并返回其返回值。回调函数内或回调函数内创建的异步操作中无法访问存储。回调函数中进行的任何 getStore() 调用都将始终返回 undefined。
【Runs a function synchronously outside of a context and returns its
return value. The store is not accessible within the callback function or
the asynchronous operations created within the callback. Any getStore()
call done within the callback function will always return undefined.】
可选的 args 会传递给回调函数。
【The optional args are passed to the callback function.】
如果回调函数抛出错误,exit() 也会抛出该错误。栈跟踪不会受到此调用的影响,并且上下文会被重新进入。
【If the callback function throws an error, the error is thrown by exit() too.
The stacktrace is not impacted by this call and the context is re-entered.】
示例:
【Example:】
// Within a call to run
try {
asyncLocalStorage.getStore(); // Returns the store object or value
asyncLocalStorage.exit(() => {
asyncLocalStorage.getStore(); // Returns undefined
throw new Error();
});
} catch (e) {
asyncLocalStorage.getStore(); // Returns the same object or value
// The error will be caught here
}