什么意思是then()在递归承诺中返回

时间:2018-09-11 21:56:39

标签: 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;
}

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 undefinedSuccess undefined?我的意思是:then中的return myFunction(random()).then((r) => console.log(Hello World ${r}));在做什么?


编辑:

我希望能在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

1 个答案:

答案 0 :(得分:3)

代码

return myFunction(random()).then((r) => console.log(`Hello World ${r}`))

将返回一个承诺,该承诺将解析为最后一个.then返回的值(即您有一个诺言链,而已解决的值是该链的结果)

在这种情况下,此值是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);
});