0

I'm trying to work with a Promise.all() function in combination with a for loop where I chained multiple promises. When I have completed the iterations and for each iteration all my promises have been resolved I want the Promise.all() to fire.

Underneath you can find my code. According to what I've read on other fora I try to push my promises to an array which would then be used in the "Promise.all()" function. I would expect my promises to be fulfilled when when I succesfully have done the "createrows" function however the promise.all() is fulfilled even before my creatrows function is executing.

Does anyone have any idea what I might be doing wrong or which misconceptions I have around the promises framework?

    for (i in scheduledRunsObjValue) {

        
    promisearray.push(
            getpropertiesObject(i,scheduledRunsObjValue).then(
            propertiesObj=>{
                utcDate=propertiesObj['startTime'];
                localDate=new Date(utcDate);
                const trackingid=propertiesObj['correlation']['clientTrackingId'];
                returnhttpinputobjectforworkflow(bearertoken,trackingid).then(HttpInputObject=>{
                    ReturnInputHttpRequestData(HttpInputObject).then(HttpRequestData=>{
                        createrows(HttpRequestData,scheduledRunsHTML,i,localDate,propertiesObj,trackingid,scheduledRunsObj).then(htmldoc=>{
                            scheduledRunsHTML=htmldoc;
                            resolve(scheduledRunsHTML);
                        })

                    })
                })
    
            })
        );

    }
    Promise.all(promisearray).then(values=>{
        resolve(scheduledRunsHTML);
        })
fvb
  • 1
  • try to wrap you code inside push in new Promise(resolve => your code and resolve(result) after it done). By the way, can't find where you define resolve method, when you push your promise in array – Иван Яковлев Mar 10 '21 at 21:33
  • @ИванЯковлев No, [absolutely do not do that](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Mar 10 '21 at 22:06
  • It looks like you've misspelled the `return` keyword twice and forgotten it once in your then handlers. – Bergi Mar 10 '21 at 22:07
  • What is `resolve(scheduledRunsHTML);` and why do you call it in multiple places? – Bergi Mar 10 '21 at 22:08
  • I use resolve(scheduledRunsHTML) after promise.all to fullfill my Promise. Then code you are seeing is just a part of another Promise. So after the iterations my "SchedulededRunsHTML" file is created and passed on to – fvb Mar 12 '21 at 17:07
  • The piece of code you are seeing is part of my function "CreateHTMLFromScheduledRuns" see underneath authenticateAndGetScheduledRuns().then(scheduledJsonObject=>{ CreateHTMLFromScheduledRuns(scheduledJsonObject,htmlJSsrc,CssSrc).then( scheduledRunsHTML=>{ panel.webview.html =scheduledRunsHTML; }) – fvb Mar 12 '21 at 17:14

0 Answers0