为了使用await
,它内部的功能需要async
.例如:
(async () => { let usersInAuth0 = []; for (let i = 0; i < 5; i++) { const usersInAuth0InPage = await getUsersFromAuth0(i); // error in webstorm if (_.isEmpty(usersInAuth0InPage)){ return res.status(200).send(usersInAuth0); } else { usersInAuth0.push(usersInAuth0InPage); } } const getUsersFromAuth0 = page => management.getUsers({per_page:100, page: page}); })();
https://codereviewvideos.com/blog/unresolved-variable-type-await/
请注意,由于management.getUsers
返回Promise,您不需要进行getUsersFromAuth0
异步 - 只需返回promise await
及其分辨率更高.
因为async
函数返回promises,所以你需要确保消耗整个代码块的任何东西能够处理它的返回值是一个promise.(您return res.status(200).send(usersInAuth0);
实际上不会返回该值 - 如果条件内部有条件,函数将返回一个解析为该值的promise )
为了使用await
,它内部的功能需要async
.例如:
(async () => { let usersInAuth0 = []; for (let i = 0; i < 5; i++) { const usersInAuth0InPage = await getUsersFromAuth0(i); // error in webstorm if (_.isEmpty(usersInAuth0InPage)){ return res.status(200).send(usersInAuth0); } else { usersInAuth0.push(usersInAuth0InPage); } } const getUsersFromAuth0 = page => management.getUsers({per_page:100, page: page}); })();
https://codereviewvideos.com/blog/unresolved-variable-type-await/
请注意,由于management.getUsers
返回Promise,您不需要进行getUsersFromAuth0
异步 - 只需返回promise await
及其分辨率更高.
因为async
函数返回promises,所以你需要确保消耗整个代码块的任何东西能够处理它的返回值是一个promise.(您return res.status(200).send(usersInAuth0);
实际上不会返回该值 - 如果条件内部有条件,函数将返回一个解析为该值的promise )