阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js domain.intercept

      Node.js domain.intercept(callback)

      callback < Function> The callback function
      Returns: < Function> The intercepted function
      

      This method is almost identical to domain.bind(callback). However, in addition to catching thrown errors, it will also intercept Error objects sent as the first argument to the function.

      In this way, the common if (err) return callback(err); pattern can be replaced with a single error handler in a single place.

      const d = domain.create();
      
      function readSomeFile(filename, cb) {
        fs.readFile(filename, 'utf8', d.intercept((data) => {
          // note, the first argument is never passed to the
          // callback since it is assumed to be the 'Error' argument
          // and thus intercepted by the domain.
      
          // if this throws, it will also be passed to the domain
          // so the error-handling logic can be moved to the 'error'
          // event on the domain instead of being repeated throughout
          // the program.
          return cb(null, JSON.parse(data));
        }));
      }
      
      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.
      });
      

      更多内容请参考:Node.js domain,或者通过 点击对应菜单 进行查看;


      目录
      本文目录
      目录