如何在JavaScript中处理嵌套的Promise?

时间:2018-06-24 01:52:23

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

我需要解决下面的困惑。

我有一个承诺清单outer_promises_list

在promise.then outer_promises_list中的每个promise中,我还有另一个promise inner_promises_list列表

我想要的是一种方法,当所有外部承诺及其内部的所有承诺都得到解决时,就会调用该方法。

这是我获取上下文的代码:

fetch = require("node-fetch") 

function all_promises_resolved_method() {
    console.log("All promises resolved ! ");
}


function start_main_process() {
    let outer_promises_list = []
    let inner_promises = []


    start_urls_list.forEach(url => {
        outer_promises_list.push(fetch(url).then(resp => resp.text()))
    })


    outer_promises_list.forEach((outer_prom) => 
        {
            outer_prom.then(htmlbody => 
            {

                inner_promises = inner_promises.concat(get_theater_promises());
                inner_promises.forEach(inner_prom => {
                    inner_prom.then(inner_resp => {

                        inner_resp.clone().text().then(inner_html_body => {
                            console.log("do synchronous stuff on this html body ");
                        })
                    })
                })
            })


            console.log("inner promises skipped , will be resolved later ");

            Promise.all(inner_promises.concat(outer_promises_list)).then(all_promises_resolved_method)

        })
}





function get_inner_promises() {
    inner_promises = []
        [url1, url2, url3].forEach((url)=>{

            inner_promises.push(fetch(href))

        })

return inner_promises;
}


start_main_process() ; 

我的问题是all_promises_resolved_method仅在第一个外部诺言的inner_promises解决时才被调用。当所有外部诺言和所有内部诺言都得到解决时,我需要一种解决方案来调用该方法。最简单,最有效的方法是什么?

1 个答案:

答案 0 :(得分:1)

似乎您在伪代码中错过了一些东西,或者有一些错字。另外,您在Promise.allouter_promises_list上分别两次致电inner_promises,不知道为什么。无论如何,以下是我的解决方案。

请记住,JS Promise旨在避免嵌套代码。因此,如果您在嵌套回调或Promises的同时使用Promise,则很可能您做得不好。

fetch = require("node-fetch")

function all_promises_resolved_method() {
    console.log("All promises resolved ! ");
}


function start_main_process() {
    let outer_promises_list = []
    let inner_promises = []

    start_urls_list.forEach(url => {
        outer_promises_list.push(fetch(url).then(resp => resp.text()))
    })


    Promise.all(outer_prom)
        .then(htmlbodyArr => {//it is an array of resolved values from all the promises in outer_prom
            /*
               I don't know how you are using htmlBody to create inner promise.
               Following is my best guess
             */

            htmlbodyArr.forEach((htmlBody)=>{
                inner_promises.push(get_theater_promises(htmlBody));
            })
            return inner_promises;
        })
        .then((inner_promises)=>{
            return Promise.all(inner_promises)
        })
        .then((innerPromiseResultArr)=>{
            all_promises_resolved_method();
        })
        .catch((err)=> console.log(err))

/*
    outer_promises_list.forEach((outer_prom) =>
    {
        outer_prom.then(htmlbody =>
        {

            inner_promises = inner_promises.concat(get_theater_promises());
            inner_promises.forEach(inner_prom => {
                inner_prom.then(inner_resp => {

                    inner_resp.clone().text().then(inner_html_body => {
                        console.log("do synchronous stuff on this html body ");
                    })
                })
            })
        })


        console.log("inner promises skipped , will be resolved later ");

        Promise.all(inner_promises.concat(outer_promises_list)).then(all_promises_resolved_method)

    })
    */
}





function get_inner_promises() {
    inner_promises = []
        [url1, url2, url3].forEach((url)=>{

        inner_promises.push(fetch(href))

    })

    return inner_promises;
}


start_main_process() ;