我是 JavaScript 新手。在下面的代码中,我是否知道为什么我仍然必须使用 return getRecipe(IDs[2]) 而不是在 .then 方法中调用 getRecipe(IDs[2]) ?甚至 getRecipe() 里面已经有 return new Promise 了?我发现如果我不在 .then 方法中使用 return,我会得到一个未定义的错误。回报实际上是回报我们下一个得到的承诺吗?但为什么以及如何?太感谢了!
const getIDs = new Promise((resolve, reject) => {
setTimeout(() => {
resolve([523, 883, 432, 974]);
}, 1500);
});
const getRecipe = recID => {
return new Promise((resolve, reject) => {
setTimeout(
ID => {
const recipe = { title: 'Fresh tomato pasta', publisher: 'Jonas' };
resolve(`${ID} : ${recipe.title}`);
},
1500,
recID
);
});
};
getIDs
.then(IDs => {
console.log(IDs);
return getRecipe(IDs[2]);
})
.then(recipe => {
console.log(recipe);
})
.catch(error => {
console.log('Error!!');
});