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
suposing
6
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;
})