node / Axios在循环中记录promises

时间:2016-10-12 14:33:40

标签: node.js promise axios

我尝试从我在堆栈溢出中找到的代码进行调整,但无济于事。我试图记录我的帖子调用的每次迭代发生的事情。承诺始终记录为挂起,我似乎无法获得最终结果。任何帮助将不胜感激。

var axios = require('axios')

url2 = `https://api.hubapi.com/properties/v1/contacts/properties?hapikey=xxx`
var mainObject = {},
    promises = [];
for(var i = 0; i < 10; i++){
    //console.log("The number i is: ",i)
    promises.push(axios.post(url2,
        {
          "name": "newcustomproperty" + i,
          "label": "A New Custom Property",
          "description": "A new property for you",
          "groupName": "contactinformation",
          "type": "string",
          "fieldType": "text",
          "formField": true,
          "displayOrder": 6,
          "options": [] 
        }

    ))
    //console.log(promises)
}
//console.log(promises)
axios.all(promises).then(function(results) {

    results.forEach(function(response) {
        console.log(response)
        mainObject[response.identifier] = response.value;

    }).catch(function(response) {
        mainObject[response.identifier] = response.value

    })
});

0 个答案:

没有答案