首页 > 解决方案 > 什么意味着 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 undefinedSuccess 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

标签: javascriptnode.jsasynchronouspromisees6-promise

解决方案


编码

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

将返回一个promise,该promise 解析为最后返回的值.then(即您有一个promise 链,并且解析的值是该链的结果)

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


推荐阅读