Node.js 如何解决此答案[AsyncFunction:response]?

Node.js 如何解决此答案[AsyncFunction:response]?,node.js,sails.js,Node.js,Sails.js,我为nodejs中的测试执行此函数 const response = async () => {return await MyService.Adding({name})} console.log(response) 但我明白了:[AsyncFunction:response] 我想使用: const res = await (async () => { const response = await MyService.Adding({name})

我为nodejs中的测试执行此函数

const response = async () => {return await MyService.Adding({name})}
console.log(response)
但我明白了:[AsyncFunction:response]

我想使用:

const res = await (async () => {
            const response = await MyService.Adding({name})
            return response
       })()
       
        console.log('RESPONDE ', res, expected)

您正在为响应分配一个函数 您希望评估函数以获得预期的响应。 大概是这样的:

let response2 = (async () => {return await MyService.Adding({name})})()
(async () => {
     const response = await MyService.Adding({name})
     console.log(response);
})()
但是,如果您正在编写一个小脚本,并且希望使用wait,那么如果没有异步函数,您就无法完成它。因此,您的脚本应该重构为如下内容:

let response2 = (async () => {return await MyService.Adding({name})})()
(async () => {
     const response = await MyService.Adding({name})
     console.log(response);
})()
您的整个脚本可以在异步函数的隐式求值中编写,等待将起作用。
这不是很简单,但是如果您使用
async
函数,那么最好使用
wait
来运行其他
async
函数

示例

const res=wait MyService.Adding({
名称
})
试试看:

//举个例子
const MyService={
添加:async(params)=>params
}
异步函数main(){
const res=wait MyService.Adding({
名字:“你的名字”
})
console.log(res)
}

main()
您可以尝试
response.then(console.log).catch(console.error)
?我知道它应该直接给你答案,但是试一下,也许它会出错?@HalilÇakar你能回答我吗?看起来你必须了解异步函数在JavaScript中是如何工作的。我发现了一篇关于异步函数的好文章:@laodemhammadalfatih我正在尝试,但不适合我,请回答我所有异步函数都会返回一个承诺。因此,当您调用异步函数时,调用方必须使用
.then()
wait
从中获取值。根本没有办法解决这个问题。另外,在
返回等待fn()
中没有任何意义。它的作用与
返回fn()
完全相同。