我正在尝试运行以下函数,该函数对 URL 数组执行 request.get,然后写入数组,整个操作需要同步。但我的代码不是同步的,每次打印不同的输出:
var arrayPart = [];
fileDecode : async function(fileName,filePath){
for (a=0; a< arr.length; a++){
var partID = JSON.parse(arr[a].id)
var uri = listID[remainder]+'/download/'+'?id='+partID
await request.get(uri, this.onRequestDone);
}
onRequestDone: async function(err, resp, body){
await new Promise(function (resolve, reject) {
if(err){
reject(err)
}else{
const buf = Buffer.from(body)
console.log("buf", buf)
arrayPart.push(buf);
fs.writeFileSync('message.txt', arrayPart)
resolve(body)
}
});
}
}
我的 onRequestDone 函数行为不正确,并且以不同的方式打印 buff。