【发布时间】:2020-05-06 21:58:33
【问题描述】:
这是承诺链。我觉得它看起来不错,但它并没有像我想要的那样工作。我已经看过了,一切似乎都井井有条。作为新手,我是否只是在 .then 的每次新迭代中重复 poem?我正在前往.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');
});
【问题讨论】:
-
记录传递给
catch的错误是个好主意。在这种情况下,错误是something went wrong ReferenceError: generateRandomDelay is not defined
标签: javascript ecmascript-6 promise es6-promise chain