Вот цепочка обещаний. Я чувствую, что он выглядит нормально, но работает не так, как я хочу. Я просмотрел, и вроде все в порядке. Поскольку я новичок в этом, могу ли я просто повторять poem
в каждой новой итерации .then
? Я делаю это в .catch
, потому что он печатает «что-то пошло не так». Я хотел бы получить любой совет!
let poem = 'But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer'
const poemJudge = (poem) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if(poem.length > 25){
console.log('We need to review this poem further');
resolve(poem);
} else {
reject('woah what? way too elementary');
}
}, generateRandomDelay());
});
};
const keepThinking = (resolvedPoem) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if(resolvedPoem.length < 45) {
console.log('terse, but we must deliberate further');
resolve(resolvedPoem);
} else {
reject('seriously? the poem is way too long!');
}
}, generateRandomDelay())
});
};
const KeepOnThinking = (secondResolvedPoem) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if(secondResolvedPoem < 40 && secondResolvedPoem > 30) {
console.log('Nailed it')
resolve(secondResolvedPoem);
} else {
reject('you are top 50 at least')
}
}, generateRandomDelay());
});
};
poemJudge(poem)
.then((resolvedPoem) => {
return keepThinking(resolvedPoem);
})
.then((secondResolvedPoem) => {
return keepOnThinking(secondResolvedPoem);
})
.then(() => {
console.log('you have completed the poem challenge');
})
.catch(() => {
console.log('something went wrong');
});