error handlingasynchronous函数没有callback或承诺

我正在使用第三方库做一些asynchronous操作,但需要尝试捕获错误。 它没有callback,也没有asynchronous函数的承诺。 有没有人知道任何图书馆或方法来捕捉这些错误? 我正在尝试做这样的事情。

var asyncLib = require( 'asyncLib' ); try { asyncLib.doSomething(); } catch( e ) { //I want to catch if doSomething throws some error // But it doesn't because it's using async resources. } 

如果您想要处理通过常用渠道无法find的错误,则可以尝试在域中运行代码。 这将启用捕获未处理的错误事件:

 var d = require('domain').create(); d.on('error', function(err) { console.error(err.stack); }); d.run(function() { yourCode(); }); 

或者,如果这是域error handling无法处理:

 process.on('uncaughtException', function (err) { console.error(err.stack); }); 

当然,你不会希望像我的例子那样只是login和继续,但你得到的照片。