gpt4 book ai didi

javascript - 跟踪/记录 promise

转载 作者:行者123 更新时间:2023-12-03 00:50:26 26 4
gpt4 key购买 nike

我正在尝试寻找跟踪 Promises 的解决方案。

我正在从事的项目有一些未等待/产生的悬空异步任务。我正在尝试查找此类情况,因为这些悬空调用会干扰测试套件。

我的方法之一是使用 SinonJS spy 来监视全局 Promise 构造函数。但是,在包装构造函数时,Promise 对象的属性会被 spy 隐藏/覆盖,从而导致 Promise 无法使用。

const spier = sinon.spy(global, 'Promise')

也许我可以利用一些全局跟踪(例如事件循环或常见的实时 Promise 数组)。

或者也许有人对 Promise 有更多的了解,并且可以推荐可访问的内部 Promise 函数的替代监视点。

想听听您是否有任何类似的需求和方法。

最佳答案

你可以像这样猴子修补 Promise 构造函数:

const global = window; // (in browser...)
const OldPromise = global.Promise;
global.Promise = class Promise extends OldPromise {
constructor(executor) {
// do whatever you want here, but must call super()
console.log('hello, promise');

super(executor); // call native Promise constructor
}
};

Promise.resolve();

来源:Monkey-patch Promise constructor

关于javascript - 跟踪/记录 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53071680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com