Javascript 区分'then'到'wait'的呼叫`
是否有某种方法可以区分Javascript 区分'then'到'wait'的呼叫`,javascript,asynchronous,async-await,proxy,Javascript,Asynchronous,Async Await,Proxy,是否有某种方法可以区分then函数的请求(或其调用)和wait 我想这样做: var myProxy = new Proxy(myPromise, { get: (t, k) => { if (k == 'then') { if (/* from await? */) console.log('from await') else console.log('without await') } }
then
函数的请求(或其调用)和wait
我想这样做:
var myProxy = new Proxy(myPromise, {
get: (t, k) => {
if (k == 'then') {
if (/* from await? */) console.log('from await')
else console.log('without await')
}
}
});
本质上,我需要区分调用然后的以下两个选项:
var a = await myProxy;
var b = myProxy.then(res => /* something */);
不,你不能,你也不应该
您可以区分调用方仅传递单个参数的直接then
调用(await
始终传递),还可以区分传递的是用户定义函数而不是某些本机解析程序函数。但是,您永远无法区分wait value
和Promise.resolve(value).then(…)
(异步()=>{
const thenable={then:console.log};
thenable.then();
然后(()=>{});
然后(res=>{},err=>{});
承诺。决心(表);
等待时机;
})();代码>“我需要区分”为什么?看起来像。此外,。然后()
在这两个实例中是相同的。它们是Promise对象的方法,没有唯一标识它们的东西。@VLAZ可以更好地理解它的工作原理。await
不应该是唯一标识的。除非您尝试检查调用链,然后反向解析源代码,并尝试在调用站点上进行分析。但是,这不是任何一个正常的应用程序都应该使用的东西,所以没有真正的需要这个功能。原则上,这部分解决了我的任务。因此,我们可以断言,如果toString()
之后的then
参数中至少有一个没有返回function(){[native code]}
对的调用,则源自用户。不幸的是,情况并非如此,因为如果用户调用例如.then(console.log,console.log)
,我们会得到相同的结果。要通过这种方式完全关闭任务,只需以字节数组或编码字符串等形式获取“[本机代码]”
。有这样的可能性吗?@Alex是fn.toString()
你在找什么?