當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js AsyncLocalStorage.exit(callback[, ...args])用法及代碼示例

asyncLocalStorage.exit(callback[, ...args])

添加於:v13.10.0、v12.17.0
Stability: 1 - 實驗性

參數

在上下文之外同步運行函數並返回其返回值。在回調函數或在回調中創建的異步操作中無法訪問存儲。在回調函數中完成的任何 getStore() 調用將始終返回 undefined

可選的args 被傳遞給回調函數。

如果回調函數拋出錯誤,錯誤也會由exit() 拋出。堆棧跟蹤不受此調用的影響,並且重新進入上下文。

例子:

// 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
}

相關用法


注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 AsyncLocalStorage.exit(callback[, ...args])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。