function populateWithKeywordsAndIds(list) {
var newList = [];
for (let i = 0; i < list.length; i++) {
//some computation goes in here that i think is not relevant
newList[x] = y;
}
fs.writeFileSync('./firstJSONWritten.js', newList);
console.log(newList);
return newList;
}
populateWithKeywordsAndIds(someList);
我运行此功能后,firstjsonwritten.js是一个空文件。populateWithKeywordsandids(somelist(的呼吁正在诺言的正文中发生。
writeFile
/ writeFileSync
未定义为接受标准数组作为数据。您可以将其传递给字符串,Buffer
或Uint8Array
,而不仅仅是一个数组。因此,您会获得奇怪的结果也就不足为奇了。( May 正在尝试将该数组转换为缓冲区,但是如果是的,它是未定义的行为,谁知道它是如何解释内容的...(
旁注:
populateWithKeywordsandids(somelist(的呼吁发生在一个。
那么,没有理由使用writeFile
的同步版本;您已经在做异步处理。改用writeFile
,以避免不必要地绑定javascript线程等待I/O。