如何以编程方式查找函数是否异步? [英] How to programmatically find if a function is async?
问题描述
我试图覆盖(猴子补丁)Jasmine框架的 it()
函数,在这个过程中,我想知道一个函数是否传递为 it()
函数的第二个参数是否为 async
类型。我尝试使用 instanceof Promise
,因为所有 async
函数都返回一个promise,但它永远不会解析为true,它永远不会进入以下代码块中的if块。我已经尝试将所有函数记录到控制台,我发现所有 async()
函数规范的返回类型为 tslib_1.awaiter(一些ARGS ..)
。
I am trying to override (monkey patch) the it()
function of the Jasmine framework and in the process, I want to know if a function which is passed as the second argument to the it()
function is of type async
or not. I tried using the instanceof Promise
because all async
functions return a promise, but it never resolves to true and it never goes into the if block in the following code block. I have tried logging all the functions to the console and I found that all the async()
function specs have a return type of tslib_1.awaiter(some args..)
.
以下是我所拥有的:
let newIt = jasmine.getEnv().it;
jasmine.getEnv().it = function(...args): jasmine.Spec {
// do something.
if(args[1] instanceOf Promise) {
debugger; // never comes in here.
// catch error.
}
return newIt.apply(this, arguments);
}
我在这里做错了什么?有谁可以请我指出正确的方向?
What am I doing wrong here? Could anyone please point me into a right direction?
谢谢。
编辑:假设我有遵循两个虚拟规范,一个是异步,另一个是同步:
Let's say I have the following two dummy specs, one is async and the other is synchronous:
异步测试:
const exp = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(1);
}, 500);
});
};
it('asyncTest', async () => {
expect(await exp()).toEqual(1);
}, 600);
同步:
it('testNon', () => {
expect(true).toBe(true);
});
推荐答案
这是XY问题。正如此答案所指出的,通常无需检查它是否为 async
功能因为这没关系。 async
function只是一个返回promise的函数。两者()=> Promise.resolve()
和 async()=> {}
应该以同样的方式处理。
This is XY problem. As pointed in this answer there is usually no need to check that it's async
function because this doesn't matter. async
function is just a function that returns a promise. Both () => Promise.resolve()
and async () => {}
should be treated in same way.
这是通过无法区分异步的事实来证实的。
和转换后的TypeScript代码中的常规函数 - 它们都只是常规函数。
This is confirmed by the fact that it's impossible to distinguish between async
and regular function in transpiled TypeScript code - both of them are just regular functions.
args [1] instanceOf Promise
不正确,因为函数不是 Promise
的实例。
Monkey-patching通常以这种方式执行(这不是特定于Jasmine或这种情况):
args[1] instanceOf Promise
is incorrect, because a function won't be an instance of Promise
.
Monkey-patching is generally performed this way (this isn't specific to Jasmine or this case):
let newIt = jasmine.getEnv().it;
jasmine.getEnv().it = function(...args): jasmine.Spec {
const fn = args[1];
let newFn;
if (fn.length > 0) {
// async spec with done param
newFn = function (done) {
const result = fn.call(this, done);
if(result instanceOf Promise)
// or even better,
// if(result && typeof result.then === 'function')
debugger;
return result;
}
} else {
newFn = function () {
const result = fn.call(this);
if(result instanceOf Promise)
debugger;
return result;
}
}
args[1] = newFn;
return newIt.apply(this, args);;
}
应该检查的是测试函数结果是 Promise的实例
(或者是否可以检查它。)
It's test function result that should be checked to be an instance of Promise
(or to be checked if it's thenable).
这篇关于如何以编程方式查找函数是否异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!