javascript - 什么意味着 then() 在递归承诺中返回
问题描述
我有以下代码:
function someAsyncOperation(){
const myArray = [1,2,3,4,5];
return Promise.resolve(myArray);
// return Promise.reject("Reject from someAsyncOperation");
}
const random = () => {
return Math.floor(Math.random() * 10) + 1;
}
const myFunction = (item) => {
return someAsyncOperation() // this function returns an array
.then((array) => {
if(!array.includes(item)){
console.log(`The element ${item} has NOT been found inside the array`);
return myFunction(random()).then((r) => console.log(`Hello World ${r}`));
}
else{
console.log(`The element ${item} has been found inside the array`);
return item;
}
});
}
myFunction(random()).then(res => {
// success
console.log("Success", res);
}).catch(err => {
// handle error here which could be either your custom error
// or an error from someAsyncOperation()
console.log("Error", err);
});
以下是其结果的一些示例:
第一个答案示例
The element 10 has NOT been found inside the array
The element 8 has NOT been found inside the array
The element 7 has NOT been found inside the array
The element 5 has been found inside the array
Hello World 5
Hello World undefined
Hello World undefined
Success undefined
答案的第二个例子
The element 9 has NOT been found inside the array
Nuevo elemento random generado 10
The element 10 has NOT been found inside the array
Nuevo elemento random generado 3
The element 3 has been found inside the array
Hello World 3
Hello World undefined
Success undefined
答案的第三个例子
The element 5 has been found inside the array
Success 5
所以,我的问题是:
为什么输出Hello World undefined
,Success undefined
有时?then
我的意思是:在做什么return myFunction(random()).then((r) => console.log(Hello World ${r}));
?
编辑:
Exaclty,我希望在return r
(下面的 JaromandaX 的答案)中不仅有找到的结果,而且还有未找到的结果的历史,按它们出现的顺序。这是我需要的一个例子:
The element 10 has NOT been found inside the array
The element 8 has NOT been found inside the array
The element 7 has NOT been found inside the array
The element 5 has been found inside the array
Hello World 10
Hello World 8
Hello World 7
Success 5
解决方案
编码
return myFunction(random()).then((r) => console.log(`Hello World ${r}`))
将返回一个promise,该promise 解析为最后返回的值.then
(即您有一个promise 链,并且解析的值是该链的结果)
在这种情况下,这个值就是undefined
console.log 返回的值
你可能想返回一个值,所以
return myFunction(random()).then((r) => (console.log(`Hello World ${r}`), r))
或者
return myFunction(random()).then((r) => {
console.log(`Hello World ${r}`);
return r;
})
将其放入您的代码中,我们得到
function someAsyncOperation(){
const myArray = [1,2,3,4,5];
return Promise.resolve(myArray);
// return Promise.reject("Reject from someAsyncOperation");
}
const random = () => {
return Math.floor(Math.random() * 10) + 1;
}
const myFunction = (item) => {
return someAsyncOperation() // this function returns an array
.then((array) => {
if(!array.includes(item)){
console.log(`The element ${item} has NOT been found inside the array`);
return myFunction(random()).then((r) => {
console.log(`Hello World ${r}`);
return r;
});
}
else{
console.log(`The element ${item} has been found inside the array`);
return item;
}
});
}
myFunction(random()).then(res => {
// success
console.log("Success", res);
}).catch(err => {
// handle error here which could be either your custom error
// or an error from someAsyncOperation()
console.log("Error", err);
});
推荐阅读
- java - 在 notifyDataSetChanged() 中禁用动画
- makefile - ocamlbuild 生成一个空白存档文件而不是可执行文件
- java - 我应该在基本页面中实例化对象 - Selenium Java
- vue.js - 如何使用 store getter 在 b-nav-item 中传递 pramas?
- javascript - After selecting all checkboxes what event gets fired? How do I recieve the data?
- jenkins - 詹金斯建立历史总是消失
- spring - 将所有未解析的端点重定向到特定的注册服务
- android - Arraylist 中的间接寻址
- r - R交叉验证lm预测函数
- python - 创建引用键或列表的python属性