Я хочу, чтобы моя программа разрешала, если все работало, а затем go к следующему тесту, используя цикл for; или, если есть ошибка, я хочу, чтобы она снова запустилась. Но если она выдаст ошибку шесть раз, я хочу, чтобы она отказалась и попробовала следующую функцию с l oop.
Что я ожидаю:
index is 0 and loop is 0
index is 1 and loop is 0
index is 2 and loop is 0
index is 3 and loop is 0
index is 4 and loop is 0
index is 5 and loop is 0
5 is too many errors
[printed error]
index is 0 and loop is 7
index is 1 and loop is 7
index is 2 and loop is 7
index is 3 and loop is 7
index is 4 and loop is 7
index is 5 and loop is 7
5 is too many errors
[printed error] .. and so on
Что я на самом деле получаю :
index is 0 and loop is 0
index is 1 and loop is 0
index is 2 and loop is 0
index is 3 and loop is 0
index is 4 and loop is 0
index is 5 and loop is 0
5 is too many errors
(node:29808) UnhandledPromiseRejectionWarning: undefined
(node:29808) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
Код:
const hello = async (index, i) => {
return new Promise(async (resolve, reject) => {
console.log(`index is ${index} and loop is ${i}`)
if(index === 5){
reject(console.log(`${index} is too many errors`)) // this runs
} else if (index === 6){
resolve(console.log("I realize it won't ever resolve :) "))
}
else{
hello(++index, i)
}
})
};
const loop_function = async () => {
return new Promise (async(res, rej)=>{
for (var i = 0; i <= 35; i += 7) {
try{
await hello(0, i)
} catch(err){
console.log("caught an error!") // this does not run
}
}
res(console.log("resolved everything")) // this does not run
})
}
const final = async () =>{
await loop_function()
console.log("loop_function complete") // this does not run
}
final();