在异步函数中使用try-catch块中的return语句的正确方式:

huangapple go评论101阅读模式
英文:

Proper way to use return statement in try, catch block for asynchronous functions

问题

以下是翻译好的部分:

  1. async function createQuiz(questions, quizDetails) {
  2. const { quizName, minPoints, maxPoints } = quizDetails;
  3. try {
  4. const quiz = await Quiz.create({
  5. quizName,
  6. minPoints,
  7. maxPoints,
  8. questions,
  9. })
  10. if (quiz) console.log("成功");
  11. } catch (e) {
  12. console.log(e.message);
  13. }
  14. }
  15. createQuiz(questions, quizDetails);
  1. async function createQuiz(questions, quizDetails) {
  2. const { quizName, minPoints, maxPoints } = quizDetails;
  3. try {
  4. const quiz = await Quiz.create({
  5. quizName,
  6. minPoints,
  7. maxPoints,
  8. questions,
  9. })
  10. if (quiz) return "成功";
  11. } catch (e) {
  12. return e.message;
  13. }
  14. }
  15. console.log(createQuiz(questions, quizDetails));

当使用 return 语句时,控制台输出 "Promise { pending }",但为什么在函数内部使用 console.log 时没有输出相同的语句呢?

英文:

I have the following code below that creates a new MongoDB document using Mongoose which does work.

  1. async function createQuiz(questions, quizDetails) {
  2. const { quizName, minPoints, maxPoints } = quizDetails;
  3. try {
  4. const quiz = await Quiz.create({
  5. quizName,
  6. minPoints,
  7. maxPoints,
  8. questions,
  9. })
  10. if (quiz) console.log("success");
  11. } catch (e) {
  12. console.log(e.message);
  13. }
  14. }
  15. createQuiz(questions, quizDetails);

However, if I replace the console.log statements with return statements shown in the code below, it suddenly stops working.

  1. async function createQuiz(questions, quizDetails) {
  2. const { quizName, minPoints, maxPoints } = quizDetails;
  3. try {
  4. const quiz = await Quiz.create({
  5. quizName,
  6. minPoints,
  7. maxPoints,
  8. questions,
  9. })
  10. if (quiz) return "success";
  11. } catch (e) {
  12. return e.message;
  13. }
  14. }
  15. console.log(createQuiz(questions, quizDetails));

With the return statements, the console logs "Promise { pending }." But why doesn't it log that same statement when the console.log was inside the function?

答案1

得分: 0

以下是翻译好的部分:

"As the comments said (thank you by the way), an async function always returns a promise so the proper way to write out the code so that it returns the value of the fulfilled promise is the following:

  1. async function createQuiz(questions, quizDetails) {
  2. const { quizName, minPoints, maxPoints } = quizDetails;
  3. try {
  4. const quiz = await Quiz.create({
  5. quizName,
  6. minPoints,
  7. maxPoints,
  8. questions,
  9. })
  10. if (quiz) return "success";
  11. } catch (e) {
  12. return e.message;
  13. }
  14. }
  15. const createdQuiz = createQuiz(questions, quizDetails);
  16. createdQuiz.then(data => console.log(data));
  17. ```"
  18. <details>
  19. <summary>英文:</summary>
  20. As the comments said (thank you by the way), an async function always returns a promise so the proper way to write out the code so that it returns the value of the fulfilled promise is the following:

async function createQuiz(questions, quizDetails) {
const { quizName, minPoints, maxPoints } = quizDetails;
try {
const quiz = await Quiz.create({
quizName,
minPoints,
maxPoints,
questions,
})
if (quiz) return "success";
} catch (e) {
return e.message;
}
}

const createdQuiz = createQuiz(questions, quizDetails);
createdQuiz.then(data => console.log(data));

  1. </details>

huangapple
  • 本文由 发表于 2023年7月18日 11:44:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定