捕获承诺中捕获的错误

时间:2020-05-08 21:01:35

标签: javascript promise

我正在尝试捕获可能在另一个promise的范围内引发的错误。 (很抱歉,这令人困惑...)

async function throwingError () {
  throw new Error('this error needs to be caught'); // I don't know if I need to return, throw or something else. This function may or may not have an error
};

async function mainFunction () {
  promiseFunction()
    .catch(error => {
      throwingError();
      // I've tried throw throwingError() and return Promise.reject(throwingError());
    });
};

mainFunction().catch(error => `caught: ${console.error}`);

现在,它无法捕获错误。
据我所知,这是行不通的,因为承诺会在不同的时间完成,但我不确定。

2 个答案:

答案 0 :(得分:1)

您的render() { const { children } = this.props; return ( <> { children ? children : <Buttons /> } </> ) } 需要返回一个Promise。您缺少的是import random Characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" Special_Characters = "~!@#$%^&*()_" Numbers = "1234567890" Chosen_Characters = [] Chosen_Special_Characters = [] Chosen_Numbers = [] Password = "" Nums_amt = 0 S_Character_amt = 0 Character_amt = 0 Total_characters = Nums_amt + S_Character_amt + Character_amt def Amount_of_Characters(): num = random.randint(1, 9) return(num) def Character_chooser(A_o_C, Char_amt, Char): global Chosen_Characters for i in range(A_o_C): Chosen_Characters += [random.choice(Char)] Char_amt += 1 return(Chosen_Characters) def Special_Character_chooser(A_o_C, S_Char_amt, S_Char): global Chosen_Special_Characters for i in range(A_o_C): Chosen_Special_Characters += [random.choice(S_Char)] S_Char_amt += 1 return(Chosen_Special_Characters) def Number_chooser(A_o_C, Num_amt, Num): global Chosen_Numbers for i in range(A_o_C): Chosen_Numbers += [random.choice(Num)] Num_amt += 1 return(Chosen_Numbers) def Assembler(A_o_C, C_c, S_C_c, N_c, Total_char, S_Char_amt, Char_amt, Num_amt, Pword): one = random.shuffle(C_c) two = random.shuffle(S_C_c) three = random.shuffle(N_c) for i in range(Total_char): chooser = random.randint(1, 3) if i + 1 <= Char_amt: if chooser == 1: temp_num = random.randint(0, len(one)) Pword += one[temp_num] two.pop(temp_num) if i + 1 <= S_Char_amt + Char_amt and i + 1 > Char_amt: if chooser == 1: temp_num = random.randint(0, len(two)) Pword += two[temp_num] two.pop(temp_num) if i + 1 > S_Char_amt + Char_amt: if chooser == 1: temp_num = random.randint(0, len(three)) Pword += three[temp_num] two.pop(temp_num) return(Pword) A = Amount_of_Characters() B = Character_chooser(Amount_of_Characters(), Character_amt, Characters) C = Special_Character_chooser(Amount_of_Characters(), S_Character_amt, Special_Characters) D = Number_chooser(Amount_of_Characters(), Nums_amt, Numbers) print("Your password is: " + Assembler(A, B, C, D, Total_characters, S_Character_amt, Character_amt, Nums_amt, Password)) 语句。您还需要在catch块中返回mainFunction

在没有return语句的情况下,您的throwingError()返回的值是一个Promise,始终会解析为return

mainFunction

答案 1 :(得分:0)

您需要返回承诺。

function throwingError () {
  throw new Error('this error needs to be caught');
}
function mainFunction () {
  return new Promise((resolve, reject) => {
    reject(throwingError())
  })
};

mainFunction().catch(error => console.log(`caught: ${error}`));