Promise.all returning empty objectsDetecting an undefined object propertyWhat is the most efficient way to...

Tikz: Perpendicular FROM a line

Is "accuse people to be racist" grammatical?

How do I draw a function along with a particular tangent line at a specific point?

How can I give a Ranger advantage on a check due to Favored Enemy without spoiling the story for the player?

Are all power cords made equal?

Is there any danger of my neighbor having my wife's signature?

Probability X1 ≥ X2

In the Lost in Space intro why was Dr. Smith actor listed as a special guest star?

Is the percentage symbol a constant?

How unreachable are Jupiter's moons from Mars with the technology developed for going to Mars?

Are there historical references that show that "diatonic" is a version of 'di-tonic' meaning 'two tonics'?

What is an explicit bijection in combinatorics?

How bad is a Computer Science course that doesn't teach Design Patterns?

What could cause an entire planet of humans to become aphasic?

Was there a pre-determined arrangment for division of Germany in case it surrendered before any Soviet forces entered its territory?

Why write a book when there's a movie in my head?

How do I narratively explain how in-game circumstances do not mechanically allow a PC to instantly kill an NPC?

Is it possible to detect 100% of SQLi with a simple regex?

Protagonist constantly has to have long words explained to her. Will this get tedious?

How can I handle players killing my NPC outside of combat?

Is there a way to pause a running process on Linux systems and resume later?

Performance and power usage for Raspberry Pi in the Stratosphere

Using time travel without creating plot holes

Can I legally make a website about boycotting a certain company?



Promise.all returning empty objects


Detecting an undefined object propertyWhat is the most efficient way to deep clone an object in JavaScript?How to check empty/undefined/null string in JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How do I test for an empty JavaScript object?How do I empty an array in JavaScript?event.preventDefault() vs. return falseHow to check if an object is an array?How do I return the response from an asynchronous call?













7















I'm trying to get multiple data objects from The Movie Database at once using Promise.all. After I loop through all the results of the fetch call, and use .json() on each bit of data, I tried to log it to the console. However, rather than an array of objects with data, I'm getting an array of Promises. Nested in the promises, I can see my data, but I'm clearly missing a step in order to have an array of data objects, instead of just Promises.



What am I missing here?



 //store movie API URLs into meaningful variables
const trending = `https://api.themoviedb.org/3/trending/all/day?api_key=${API_KEY}`;
const topRated = `https://api.themoviedb.org/3/movie/top_rated?api_key=${API_KEY}&language=en-US&page=1`;
const nowPlaying = `https://api.themoviedb.org/3/movie/now_playing?api_key=${API_KEY}&language=en-US&page=1`;
const upcoming = `https://api.themoviedb.org/3/movie/upcoming?api_key=${API_KEY}&language=en-US&page=1`;
//create an array of urls to fetch data from
const allMovieURLs = [trending, topRated, nowPlaying, upcoming];
const promiseURLs = allMovieURLs.map(url => fetch(url));
Promise.all(promiseURLs)
.then(responses => responses.map(url => url.json()))
.then(dataArr => console.log(dataArr));
};









share|improve this question



























    7















    I'm trying to get multiple data objects from The Movie Database at once using Promise.all. After I loop through all the results of the fetch call, and use .json() on each bit of data, I tried to log it to the console. However, rather than an array of objects with data, I'm getting an array of Promises. Nested in the promises, I can see my data, but I'm clearly missing a step in order to have an array of data objects, instead of just Promises.



    What am I missing here?



     //store movie API URLs into meaningful variables
    const trending = `https://api.themoviedb.org/3/trending/all/day?api_key=${API_KEY}`;
    const topRated = `https://api.themoviedb.org/3/movie/top_rated?api_key=${API_KEY}&language=en-US&page=1`;
    const nowPlaying = `https://api.themoviedb.org/3/movie/now_playing?api_key=${API_KEY}&language=en-US&page=1`;
    const upcoming = `https://api.themoviedb.org/3/movie/upcoming?api_key=${API_KEY}&language=en-US&page=1`;
    //create an array of urls to fetch data from
    const allMovieURLs = [trending, topRated, nowPlaying, upcoming];
    const promiseURLs = allMovieURLs.map(url => fetch(url));
    Promise.all(promiseURLs)
    .then(responses => responses.map(url => url.json()))
    .then(dataArr => console.log(dataArr));
    };









    share|improve this question

























      7












      7








      7








      I'm trying to get multiple data objects from The Movie Database at once using Promise.all. After I loop through all the results of the fetch call, and use .json() on each bit of data, I tried to log it to the console. However, rather than an array of objects with data, I'm getting an array of Promises. Nested in the promises, I can see my data, but I'm clearly missing a step in order to have an array of data objects, instead of just Promises.



      What am I missing here?



       //store movie API URLs into meaningful variables
      const trending = `https://api.themoviedb.org/3/trending/all/day?api_key=${API_KEY}`;
      const topRated = `https://api.themoviedb.org/3/movie/top_rated?api_key=${API_KEY}&language=en-US&page=1`;
      const nowPlaying = `https://api.themoviedb.org/3/movie/now_playing?api_key=${API_KEY}&language=en-US&page=1`;
      const upcoming = `https://api.themoviedb.org/3/movie/upcoming?api_key=${API_KEY}&language=en-US&page=1`;
      //create an array of urls to fetch data from
      const allMovieURLs = [trending, topRated, nowPlaying, upcoming];
      const promiseURLs = allMovieURLs.map(url => fetch(url));
      Promise.all(promiseURLs)
      .then(responses => responses.map(url => url.json()))
      .then(dataArr => console.log(dataArr));
      };









      share|improve this question














      I'm trying to get multiple data objects from The Movie Database at once using Promise.all. After I loop through all the results of the fetch call, and use .json() on each bit of data, I tried to log it to the console. However, rather than an array of objects with data, I'm getting an array of Promises. Nested in the promises, I can see my data, but I'm clearly missing a step in order to have an array of data objects, instead of just Promises.



      What am I missing here?



       //store movie API URLs into meaningful variables
      const trending = `https://api.themoviedb.org/3/trending/all/day?api_key=${API_KEY}`;
      const topRated = `https://api.themoviedb.org/3/movie/top_rated?api_key=${API_KEY}&language=en-US&page=1`;
      const nowPlaying = `https://api.themoviedb.org/3/movie/now_playing?api_key=${API_KEY}&language=en-US&page=1`;
      const upcoming = `https://api.themoviedb.org/3/movie/upcoming?api_key=${API_KEY}&language=en-US&page=1`;
      //create an array of urls to fetch data from
      const allMovieURLs = [trending, topRated, nowPlaying, upcoming];
      const promiseURLs = allMovieURLs.map(url => fetch(url));
      Promise.all(promiseURLs)
      .then(responses => responses.map(url => url.json()))
      .then(dataArr => console.log(dataArr));
      };






      javascript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      WonkasWillyWonkasWilly

      22229




      22229
























          2 Answers
          2






          active

          oldest

          votes


















          5














          Your .then(responses => responses.map(url => url.json())) resolves to an array of Promises, so you need to call Promise.all again if you want to wait for all to resolve:



          Promise.all(promiseURLs)
          .then(responses => Promise.all(responses.map(url => url.json())))
          .then(dataArr => console.log(dataArr));


          Or, you might consider using just one Promise.all, and having each URL fetch and the json, that way some items aren't idle in the middle of script execution:



          const allMovieURLs = [trending, topRated, nowPlaying, upcoming];
          const promiseURLs = allMovieURLs.map(url => fetch(url).then(res => res.json()));
          Promise.all(promiseURLs)
          .then(dataArr => console.log(dataArr));





          share|improve this answer
























          • I didn't realize that Body.json also returned a Promise! Your explanation was simple and concise, and this has solved my problem. Thank you.

            – WonkasWilly
            48 mins ago



















          3














          try doing it this way



          const promiseURLs = allMovieURLs.map(url => fetch(url).then(res => res.json()));
          Promise.all(promiseURLs)
          .then(responses => responses.forEach(response => { console.log(response)})





          share|improve this answer








          New contributor




          George.S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.




















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54850431%2fpromise-all-returning-empty-objects%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            5














            Your .then(responses => responses.map(url => url.json())) resolves to an array of Promises, so you need to call Promise.all again if you want to wait for all to resolve:



            Promise.all(promiseURLs)
            .then(responses => Promise.all(responses.map(url => url.json())))
            .then(dataArr => console.log(dataArr));


            Or, you might consider using just one Promise.all, and having each URL fetch and the json, that way some items aren't idle in the middle of script execution:



            const allMovieURLs = [trending, topRated, nowPlaying, upcoming];
            const promiseURLs = allMovieURLs.map(url => fetch(url).then(res => res.json()));
            Promise.all(promiseURLs)
            .then(dataArr => console.log(dataArr));





            share|improve this answer
























            • I didn't realize that Body.json also returned a Promise! Your explanation was simple and concise, and this has solved my problem. Thank you.

              – WonkasWilly
              48 mins ago
















            5














            Your .then(responses => responses.map(url => url.json())) resolves to an array of Promises, so you need to call Promise.all again if you want to wait for all to resolve:



            Promise.all(promiseURLs)
            .then(responses => Promise.all(responses.map(url => url.json())))
            .then(dataArr => console.log(dataArr));


            Or, you might consider using just one Promise.all, and having each URL fetch and the json, that way some items aren't idle in the middle of script execution:



            const allMovieURLs = [trending, topRated, nowPlaying, upcoming];
            const promiseURLs = allMovieURLs.map(url => fetch(url).then(res => res.json()));
            Promise.all(promiseURLs)
            .then(dataArr => console.log(dataArr));





            share|improve this answer
























            • I didn't realize that Body.json also returned a Promise! Your explanation was simple and concise, and this has solved my problem. Thank you.

              – WonkasWilly
              48 mins ago














            5












            5








            5







            Your .then(responses => responses.map(url => url.json())) resolves to an array of Promises, so you need to call Promise.all again if you want to wait for all to resolve:



            Promise.all(promiseURLs)
            .then(responses => Promise.all(responses.map(url => url.json())))
            .then(dataArr => console.log(dataArr));


            Or, you might consider using just one Promise.all, and having each URL fetch and the json, that way some items aren't idle in the middle of script execution:



            const allMovieURLs = [trending, topRated, nowPlaying, upcoming];
            const promiseURLs = allMovieURLs.map(url => fetch(url).then(res => res.json()));
            Promise.all(promiseURLs)
            .then(dataArr => console.log(dataArr));





            share|improve this answer













            Your .then(responses => responses.map(url => url.json())) resolves to an array of Promises, so you need to call Promise.all again if you want to wait for all to resolve:



            Promise.all(promiseURLs)
            .then(responses => Promise.all(responses.map(url => url.json())))
            .then(dataArr => console.log(dataArr));


            Or, you might consider using just one Promise.all, and having each URL fetch and the json, that way some items aren't idle in the middle of script execution:



            const allMovieURLs = [trending, topRated, nowPlaying, upcoming];
            const promiseURLs = allMovieURLs.map(url => fetch(url).then(res => res.json()));
            Promise.all(promiseURLs)
            .then(dataArr => console.log(dataArr));






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 1 hour ago









            CertainPerformanceCertainPerformance

            88.5k154775




            88.5k154775













            • I didn't realize that Body.json also returned a Promise! Your explanation was simple and concise, and this has solved my problem. Thank you.

              – WonkasWilly
              48 mins ago



















            • I didn't realize that Body.json also returned a Promise! Your explanation was simple and concise, and this has solved my problem. Thank you.

              – WonkasWilly
              48 mins ago

















            I didn't realize that Body.json also returned a Promise! Your explanation was simple and concise, and this has solved my problem. Thank you.

            – WonkasWilly
            48 mins ago





            I didn't realize that Body.json also returned a Promise! Your explanation was simple and concise, and this has solved my problem. Thank you.

            – WonkasWilly
            48 mins ago













            3














            try doing it this way



            const promiseURLs = allMovieURLs.map(url => fetch(url).then(res => res.json()));
            Promise.all(promiseURLs)
            .then(responses => responses.forEach(response => { console.log(response)})





            share|improve this answer








            New contributor




            George.S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

























              3














              try doing it this way



              const promiseURLs = allMovieURLs.map(url => fetch(url).then(res => res.json()));
              Promise.all(promiseURLs)
              .then(responses => responses.forEach(response => { console.log(response)})





              share|improve this answer








              New contributor




              George.S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.























                3












                3








                3







                try doing it this way



                const promiseURLs = allMovieURLs.map(url => fetch(url).then(res => res.json()));
                Promise.all(promiseURLs)
                .then(responses => responses.forEach(response => { console.log(response)})





                share|improve this answer








                New contributor




                George.S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.










                try doing it this way



                const promiseURLs = allMovieURLs.map(url => fetch(url).then(res => res.json()));
                Promise.all(promiseURLs)
                .then(responses => responses.forEach(response => { console.log(response)})






                share|improve this answer








                New contributor




                George.S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                share|improve this answer



                share|improve this answer






                New contributor




                George.S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered 1 hour ago









                George.SGeorge.S

                415




                415




                New contributor




                George.S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                George.S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                George.S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54850431%2fpromise-all-returning-empty-objects%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Szabolcs (Ungheria) Altri progetti | Menu di navigazione48°10′14.56″N 21°29′33.14″E /...

                    Discografia di Klaus Schulze Indice Album in studio | Album dal vivo | Singoli | Antologie | Colonne...

                    How to make inet_server_addr() return localhost in spite of ::1/128RETURN NEXT in Postgres FunctionConnect to...