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


Node.js Domain.bind(callback)用法及代碼示例

domain.bind(callback)

返回的函數將是提供的回調函數的包裝器。當調用返回的函數時,拋出的任何錯誤都將被路由到域的'error' 事件。

const d = domain.create();

function readSomeFile(filename, cb) {
  fs.readFile(filename, 'utf8', d.bind((er, data) => {
    // If this throws, it will also be passed to the domain.
    return cb(er, data ? JSON.parse(data) : null);
  }));
}

d.on('error', (er) => {
  // An error occurred somewhere. If we throw it now, it will crash the program
  // with the normal line number and stack message.
});

相關用法


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