阿西河

所有教程

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

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js domain.run

      Node.js domain.run(fn[, ...args])

      • fn < Function>
      • …args < any>

      Run the supplied function in the context of the domain, implicitly binding all event emitters, timers, and lowlevel requests that are created in that context. Optionally, arguments can be passed to the function.

      This is the most basic way to use a domain.

      const domain = require('domain');
      const fs = require('fs');
      const d = domain.create();
      d.on('error', (er) => {
        console.error('Caught error!', er);
      });
      d.run(() => {
        process.nextTick(() => {
          setTimeout(() => { // simulating some various async stuff
            fs.open('non-existent file', 'r', (er, fd) => {
              if (er) throw er;
              // proceed...
            });
          }, 100);
        });
      });
      

      In this example, the d.on(‘error’) handler will be triggered, rather than crashing the program.


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


      目录
      本文目录
      目录