阿西河

所有教程

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

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js domain 与 Promise

      Node.js domain 与 Promise

      As of Node.js 8.0.0, the handlers of Promises are run inside the domain in which the call to .then() or .catch() itself was made:

      const d1 = domain.create();
      const d2 = domain.create();
      
      let p;
      d1.run(() => {
        p = Promise.resolve(42);
      });
      
      d2.run(() => {
        p.then((v) => {
          // running in d2
        });
      });
      

      A callback may be bound to a specific domain using domain.bind(callback):

      const d1 = domain.create();
      const d2 = domain.create();
      
      let p;
      d1.run(() => {
        p = Promise.resolve(42);
      });
      
      d2.run(() => {
        p.then(p.domain.bind((v) => {
          // running in d1
        }));
      });
      

      Note that domains will not interfere with the error handling mechanisms for Promises, i.e. no ‘error’ event will be emitted for unhandled Promise rejections.


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


      目录
      本文目录
      目录