Javascript 在递归承诺中返回then()是什么意思
我有以下代码:Javascript 在递归承诺中返回then()是什么意思,javascript,node.js,asynchronous,promise,es6-promise,Javascript,Node.js,Asynchronous,Promise,Es6 Promise,我有以下代码: 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; }
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);
});
下面是它的结果的一些例子:
答案的第一个示例
第二个答案示例
第三个答案示例
所以,我的问题是: 为什么有时输出未定义的Hello World和未定义的Success?我的意思正是:在
中执行然后返回myFunction(random())。然后((r)=>console.log(helloworld${r}))代码>
编辑:
例如,我希望在返回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}`))
将返回解析为上一个中返回的值的承诺。然后
(即,您有一个承诺链,解析的值是该链的结果)
在这种情况下,该值是未定义的
,与console.log返回的值相同
您可能想要返回一个值,所以
return myFunction(random()).then((r) => (console.log(`Hello World ${r}`), r))
或
把它放到你的代码里,我们就可以
函数someAsyncOperation(){
常量myArray=[1,2,3,4,5];
返回Promise.resolve(myArray);
//return Promise.reject(“从某个异步操作中拒绝”);
}
常量随机=()=>{
返回Math.floor(Math.random()*10)+1;
}
常量myFunction=(项)=>{
return someAsyncOperation()//此函数返回一个数组
。然后((数组)=>{
如果(!array.includes(项目)){
log(`The element${item}未在数组`中找到);
返回myFunction(random())。然后((r)=>{
log(`helloworld${r}`);
返回r;
});
}
否则{
log(`element${item}已在数组`中找到);
退货项目;
}
});
}
myFunction(random())。然后(res=>{
//成功
console.log(“Success”,res);
}).catch(错误=>{
//在此处处理错误,可能是您的自定义错误
//或者来自someAsyncOperation()的错误
日志(“错误”,err);
});
即,如果找到的最后一个元素是4
,那么当未找到元素时,为什么代码会在其他中返回Hello World 4
???我的意思是,为什么没有返回,即Hello World 6
Hello World 8
suposing6
和8
元素没有找到???@robe007-你期望什么输出?我编辑了这篇文章,向你展示了我期望的输出。如果你能看一看,会很有帮助的!
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}`))
return myFunction(random()).then((r) => (console.log(`Hello World ${r}`), r))
return myFunction(random()).then((r) => {
console.log(`Hello World ${r}`);
return r;
})