使新的 stacktrace.js 库同步

Make the new stacktrace.js library synchronous

我正在使用新的堆栈跟踪 js 库,它 returns 一个承诺。

StackTrace.get() // this results in a promise

我可以做些什么来让它同步吗?

像这样:

var result = magicalSomething(StackTrace.get());

更新:现在有一个用于堆栈跟踪的 .getSync(),它提供了部分信息,但您可以使用它。

StackTrace.js 由 making calls to the script files involved in the stack trace and extracting additional information for them. This means that you cannot obtain the stack trace it generates since a part is already asynchronous 创作。

从技术上讲,这实际上是可以解决的并且可以同步(以冻结页面数十秒为代价)——但这似乎不是库的设计目标。

相反,使用库提供的内容并使用给定的承诺:

StackTrace.get().then(function(result) {
   // I got result here
});

更新:本杰明的回答更正确。从 stacktrace.js v1.3.0

开始使用
var stacktrace = StackTrace.getSync();

如果您需要同步行为并且不关心猜测匿名函数或源映射支持或旧 IE,您可以只使用作为 stacktrace.js - error-stack-parser 基础的堆栈解析库方式:

function stacktrace() {
  try {
    throw new Error();
  } catch (e) {
    return ErrorStackParser.parse(e);
  }
}

只是想提一下,也可以使用 stacktrace-js 库同步获取源映射堆栈跟踪。

它不是 API 的一部分,但可以通过一些修改实现。

看这里:https://github.com/stacktracejs/stacktrace.js/issues/188