[英]Await / async on anonymous function
I'm trying to use await on an anonymous function here are the results : 我试图在匿名函数上使用await这是结果:
This is the way that works 这是有效的方式
async function hello(){
return "hello";
}
let x = await hello();
console.log(x);
result : 结果:
"Hello"
“你好”
This is the way i want it to work : 这是我希望它工作的方式:
let x = await async function() {return "hello"};
console.log(x);
result : 结果:
[AsyncFunction]
[AsyncFunction]
What am i missing ? 我想念什么? I'm new to promises.
我是新来的诺言。
EDIT : I tried adding the () after the anonymous function to call it. 编辑:我试图添加匿名函数后调用()。 Here is the example with the actual Async code :
这是带有实际异步代码的示例:
let invitationFound = await (async function (invitationToken, email){
return models.usersModel.findOneInvitationByToken(invitationToken, email)
.then(invitationFound => {
return invitationFound;
})
.catch(err =>{
console.log(err);
});
})();
console.log(invitationFound);
return res.status(200).json({"oki " : invitationFound});
Result of the console.log : console.log的结果:
ServerResponse { domain: null, _events: { finish: [Function: bound resOnFinish] }, _eventsCount: 1, _maxListeners: undefined, output: [], outputEncodings: [], .....
ServerResponse {域:空,_events:{完成:[功能:绑定resOnFinish]},_eventsCount:1,_maxListeners:未定义,输出:[],outputEncodings:[],..
Result of the res.code.. res.code的结果
handledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
handlePromiseRejectionWarning:TypeError:将圆形结构转换为JSON
I don't think the error come from models.usersModel.findOneInvitationByToken because it works fine when i use it in the first case 我不认为该错误来自models.usersModel.findOneInvitationByToken,因为在我的第一种情况下使用它时效果很好
let userFound = await test(invitationToken, email);
EDIT 2 : 编辑2:
I found out the second problem ! 我发现了第二个问题! I forgot to put the parameters into the parenthesis
我忘了把参数放在括号里
let invitationFound = await (async function (invitationToken, email){
return models.usersModel.findOneInvitationByToken(invitationToken, email)
.then(invitationFound => {
return invitationFound;
})
.catch(err =>{
console.log(err);
});
})(invitationToken, email);
console.log(invitationFound);
return res.status(200).json({"oki " : invitationFound});
result : 结果:
{ oki : {mydata} }
{oki:{mydata}}
You await Promises , which are returned from async functions, not the async function itself. 你等待的承诺 ,这是从异步函数返回 ,而不是异步函数本身。 Just add a call:
只需添加一个电话:
let x = await (async function() {return "hello"})();
console.log(x);
// or
console.log(await (async() => 'hello')())
You are not calling a function in the second case: 在第二种情况下,您没有调用函数:
let x = await hello();
This is how you are accessing it in the first case but in the second case, you are just you are adding await to a function declaration. 这是您在第一种情况下访问它的方式,但是在第二种情况下,您只是将await添加到函数声明中。 It is just returning function, you need to change it to
它只是返回函数,您需要将其更改为
let x = await (async function() {return "hello"})();
console.log(x);
You can use Promise and await like this: 您可以这样使用Promise并等待:
async abc function() {
return new Promise(resolve => {
setTimeout(() => {
resolve('hello');
}, 2000);
});
}
let x = await abc();
console.log(x);
// OR
let y = await (async function() {return "hello"})();
console.log(y);
// OR
console.log(await (async() => 'hello')())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.