带for循环的异步函数等待http.request

时间:2020-08-13 06:08:37

标签: javascript node.js arrays ecmascript-6 async-await

我正在尝试运行以下脚本来调用我的api 3次(遍历数组),并使用POST方法创建新记录。我在日志中看不到api调用已启动。谁能看到我要去哪里错了?

var tens = "rec1, rec2, re3"
console.log(tens);

var letters = tens.split(',').map(string=>string.trim());
console.log(letters);

async function callAPIs(letters) {
    responses = [];
    for (let i = 0; i < letters.length; i++) {
        var apiRequest = await http.request({
            'endpoint': 'test',
            'path':'/api/table/records', 
            'method': 'POST',
            "headers": {
                "Authorization": "Basic xxxxxxxxx=",
                "Content-Type": "application/json"
            }
        })
        apiRequest.send(data)
        apiRequest.end((data) => {
            responses.push(data)
        });
    };
    return responses
 }

var data = {};    
var caller_id = "user1";
data.caller_id = caller_id;
console.log(caller_id);

0 个答案:

没有答案
相关问题