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

What do "compile" , "fit" and "predict" do in Keras sequential models?

Process substitution inside a subshell to set a variable

How can I put a period right after the algorithm's number in the algorithm's title?

Third wheel character

If a 12 by 16 sheet of paper is folded on its diagonal, what is the area of the region of the overlap?

What's the reason that we have different quantities of days each month?

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

Why is it that Bernie Sanders is always called a "socialist"?

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

What is wrong with my use of "find -print0"?

Is practicing on a digital piano harmful to an experienced piano player?

Can you say "leftside right"?

Performance and power usage for Raspberry Pi in the Stratosphere

Why did Ylvis use "go" instead of "say" in phrases like "Dog goes 'woof'"?

Using time travel without creating plot holes

Why does a single AND gate need 60 transistors?

How to deal with an underperforming subordinate?

What is an efficient way to digitize a family photo collection?

Minimum Viable Product for RTS game?

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

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

How can I differentiate duration vs starting time

Why don't you get burned by the wood benches in a sauna?

Can you prevent a man in the middle from reading the message?



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 53 mins 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
            35 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
              35 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
              35 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 51 mins 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
              35 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
              35 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
            35 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
            35 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 48 mins ago









                George.SGeorge.S

                414




                414




                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

                    Щит и меч (фильм) Содержание Названия серий | Сюжет |...

                    Венесуэла на летних Олимпийских играх 2000 Содержание Состав...

                    Meter-Bus Содержание Параметры шины | Стандартизация |...