跟踪/记录承诺 [英] Tracking/Logging Promises

查看:62
本文介绍了跟踪/记录承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试寻找跟踪 Promise 的解决方案.

I am trying to find a solution for tracking Promises.

我正在处理的项目有一些悬而未决的异步任务,它们没有等待/产生.我试图找到这样的情况,因为这些悬空调用会干扰测试套件.

The project that I am working on has some dangling async tasks that are not awaited/yielded. I am trying to find such cases, as these dangling calls are interfering with test suites.

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

One of my approaches was to spy on global Promise constructor with SinonJS spies. But while wrapping constructor, properties of Promise object gets hidden/overwritten by spy, rendering Promises unusable.

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

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

Perhaps there is some global tracking that I could exploit (such as event loop, or common array of live Promises).

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

Or maybe somebody has a bit more insight into Promises and can recommend alternative spying point on accessible internal Promise functions.

想知道您是否有任何类似的需求和方法.

Would like to hear If you had any similar needs and your approaches.

推荐答案

你可以像这样修改 promise 的构造函数:

You can monkey patch the promise constructor like this:

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 构造函数

这篇关于跟踪/记录承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆