Promise.All返回空对象

时间:2019-02-24 09:15:29

标签: javascript

我正在尝试使用Promise.all从电影数据库中一次获取多个数据对象。在遍历fetch调用的所有结果并在数据的每一位上使用.json()之后,我尝试将其记录到控制台。但是,我得到的是Promises数组,而不是带有数据的对象数组。嵌套在Promise中,我可以看到我的数据,但是我显然缺少步骤以拥有一组数据对象,而不仅仅是Promises

我在这里想念什么?

 //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));
  };

2 个答案:

答案 0 :(得分:18)

您的cd 'G:\' set view map set dgrid3d set ytics font "Times New Roman,12" set xtics font "Times New Roman,12" unset key set palette rgbformulae 30,31,32 set decimalsign ',' set format y "%5.1f" set format x "%5.1f" splot "mapa.txt" using 1:2:3 with pm3d 解析为一组Promises,因此,如果要等待所有人解析,则需要再次致电.then(responses => responses.map(url => url.json()))

Promise.all

或者,您可以考虑只使用一个Promise.all(promiseURLs) .then(responses => Promise.all(responses.map(url => url.json()))) .then(dataArr => console.log(dataArr)); ,并使用每个URL Promise.allfetch,这样在脚本执行过程中某些项目就不会闲置:< / p>

json

答案 1 :(得分:4)

尝试通过这种方式

4.1.1