Axios-在多个foreach中等待响应

时间:2019-04-15 15:28:12

标签: javascript axios

我正在尝试使用多个foreach迭代创建axios请求(GET)。迭代使用名称,ID和时间戳。该代码显然工作,但我需要等待响应,然后继续循环。实际代码全部启动请求,然后等待响应,我需要启动一个请求,然后等待响应,然后再次启动

     var array = ['AA', 'BB', 'CC'];
     var dates = [
         [1514761200000, 1515970800000],
         [1515970800000, 1517439600000],
         [1517439600000, 1518649200000],
         [1518649200000, 1519858800000],
         [1519858800000, 1521068400000],
         [1521068400000, 1522533600000],
         [1522533600000, 1523743200000],
         [1523743200000, 1525125600000],
         [1525125600000, 1526335200000],
         [1526335200000, 1527804000000],
         [1527804000000, 1529013600000],
         [1529013600000, 1530396000000],
         [1530396000000, 1531605600000],
         [1531605600000, 1533074400000],
         [1533074400000, 1534284000000],
         [1534284000000, 1535752800000],
         [1535752800000, 1536962400000],
         [1536962400000, 1538344800000],
         [1538344800000, 1539554400000],
         [1539554400000, 1541026800000],
         [1541026800000, 1542236400000],
         [1542236400000, 1543618800000],
         [1543618800000, 1544828400000],
         [1544828400000, 1546297200000],
     ];

     LOOP

     array.forEach(async function (data) {
         for (i = 1; i < 50; i++) {
             dates.forEach(async function (data2) {
                 await axios({
                         method: 'get',
                         url: 'https://' + data +
                             'MYURL=' +
                             i + '&from=' + data2[0] + '&to=' +
                             data2[1],
                     })
                     .then(function (response) {
                         console.log(response)
                     })
                     .catch(function (error) {
                         console.log(error);
                     });
             });
         }


});

所以¿每个人都知道我如何以编程方式(在多个循环中)启动请求?

0 个答案:

没有答案