N
N
Nadim Zakirov2021-07-05 09:24:09
JavaScript
Nadim Zakirov, 2021-07-05 09:24:09

How to return an error in a promise?

What should I return inside the check3 function so that the execution of the code immediately jumps to the catch block ?
By immediately, it means jumping to catch without subsequently executing the check4 and check5 functions .

async function sequence() {
  
  try {
    
    await check1();
    await check2();
    await check3();
    await check4();
    await check5();
    
    return {
      'success': true,
      'message': 'Проверка прошла успешно.'
    };
    
  }
  
  catch(err) {
    
    return {
      'success': false,
      'message': 'Ошибка: ' + err
    };
    
  }
  
}

async function check3() {
  return ???;
}


PS Somehow it was not possible to work with the emergence of errors before, as is usually done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2021-07-05
@zkrvndm

Do not return - discard)

const check3 = () => {
  if (true) {
    throw "Че-т не получилось";
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question