简体   繁体   English

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

[英]how to handle nested promises in javascript?

I need a solution to this below confusion i have . 我需要解决下面的困惑。

I have a list of promises outer_promises_list . 我有一个诺言的清单outer_promises_list

Inside the promise.then of each promise in this outer_promises_list , I have another list of promises inner_promises_list outer_promises_list里面的每个promise中,我还有另一个promise列表inner_promises_list

What I want is a method which gets called when all the outer promises and all the promises inside them get resolved . 我想要的是一个方法,当所有外部承诺及其内部的所有承诺都得到解决时,该方法将被调用。

Here is my code to get the context : 这是我获取上下文的代码:

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

My issue is that all_promises_resolved_method is called only when the inner_promises of the first outer promise is resolved . 我的问题是,只有在解决第一个外部诺言的inner_promises时, all_promises_resolved_method调用all_promises_resolved_method I need a solution to call that method when all outer promises and each of all of those inner promises are resolved. 当所有外部诺言和所有内部诺言都得到解决时,我需要一种解决方案来调用该方法。 What's the easiest way and efficient way to do this ? 最简单,最有效的方法是什么?

It seems you missed a few things in your pseudocode or there is some typo. 看来您错过了伪代码中的某些内容,或者有一些错字。 Also, you are calling Promise.all on outer_promises_list and inner_promises twice, not sure why. 另外,您要在outer_promises_listinner_promises两次调用Promise.all ,不知道为什么。 Following is my solution, anyway. 无论如何,以下是我的解决方案。

Please remember that JS Promise is designed to avoid nested code. 请记住,JS Promise旨在避免嵌套代码。 So if you are using promises while nesting callbacks or Promises, most likely you are not doing it right. 因此,如果您在嵌套回调或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() ; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM