gpt4 book ai didi

javascript - 从 Promise.then/promise.catch 中获取原始函数名称

转载 作者:行者123 更新时间:2023-11-30 08:21:33 24 4
gpt4 key购买 nike

我有一个返回 promise 的函数,如下所示。从 Promise 回调(.then.catch)中,我希望能够找到原始函数的名称("myFunction" ). 我该怎么做?

function myFunction() {
return fetch('https://example.com')
// The promise will fail because Stackoverflow is sandboxed
.catch(function() {
console.log(JSON.stringify(arguments.callee.name))
// It is returning the name of my anonymous function ("").
// I want it to log "myFunction" instead.
})
}

myFunction();

最佳答案

您可以将参数保存到catch闭包之外的另一个变量中。

function myFunction() {
let args = arguments;
return fetch('https://example.com')
// The promise will fail because Stackoverflow is sandboxed
.catch(function() {
console.log(JSON.stringify(args.callee.name))
// It is returning the name of my anonymous function ("").
// I want it to log "myFunction" instead.
})
}

myFunction();

关于javascript - 从 Promise.then/promise.catch 中获取原始函数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52752325/

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