【问题标题】:What means a then() returned inside a recursive promise什么意味着 then() 在递归承诺中返回
【发布时间】:2019-02-16 11:28:20
【问题描述】:

我有以下代码:

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


编辑:

Exaclty,我希望在return rJaromandaX 的回答如下)中不仅有找到的结果,还有历史未找到结果按它们出现的顺序排列。这是我需要的一个例子:

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

【问题讨论】:

    标签: javascript node.js asynchronous promise es6-promise


    【解决方案1】:

    代码

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

    【讨论】:

    • 如果找到的最后一个元素是4,为什么你的代码在其他then没有找到元素时返回Hello World 4?我的意思是,为什么不返回 i.e Hello World 6 Hello World 8 假设 68 是未找到的元素??
    • @robe007 - 你期望什么输出?
    • 我编辑了这篇文章,向您展示我期望的输出。如果你能看看,会有所帮助!
    猜你喜欢
    • 2020-04-27
    • 2021-08-29
    • 2018-10-11
    • 2021-04-30
    • 2016-03-09
    • 2019-05-25
    • 2020-09-04
    • 2017-07-24
    • 1970-01-01
    相关资源
    最近更新 更多