如何获取Object.键迭代通过数组对象等待异步获取的API结果在每次迭代结束?



此节点代码

  1. 读取一些本地数据fs.readFile('./local.json', 'utf8', (err, data) => {

  2. 打开一个尝试{

  3. 解析数据,然后稍微变平,const json_data = JSON.parse(data);

  4. 循环生成的数组并收集数据点

  5. 在循环中,代码提取url并为每次迭代发送两个api请求

  6. api请求是链式承诺,返回json

  7. 返回的数据被检查和处理

  8. 在循环的每次迭代中,使用三元运算符和条件从3个源中选择数据

  9. 然后使用首选数据点组成字符串字量

  10. 在fetch承诺链的末尾,就在下一次循环之前,正确组合的字符串字量被打印到文件

  11. 只是它们没有正确组合…

  12. 循环不知道足够等待承诺解决,所以第一次迭代的数据被写入后续迭代

  13. 这里没有显示一些代码,例如辅助函数和变量定义、字符串字面量和catch块,但这或多或少是结构:

fs.readFile('./local.json', 'utf8', (err, data) => {
try {
// parse data and extract api endpoints
const process_local_data = JSON.parse(data);
//define functions and variables
const functions_to_check_and_process_local_data = (data) => { //...
}
const functions_to_check_and_process_api_data = (data) => { //...
}
//define first api fetcher
const first_api_fetcher = async(urla, urlb) => {
const contentType = response.headers.get("content-type");
const response = await fetch(urla);
if (contentType && contentType.indexOf("application/json") !== -1) {
let results = await response.json();
functions_to_check_and_process_api_data(results);
}
await second_api_checker(urlb); //the second api request is chained to a promise in the first one
} //end first api fetcher

//define_second_api_fetcher
const second_api_fetcher = async(url) => {
const contentType = response.headers.get("content-type");
const response = await fetch(url);
if (contentType && contentType.indexOf("application/json") !== -1) {
let results = await response.json();
functions_to_check_and_process_api_data(results);
}
} //end second api checker
//run the processing loop
Object.keys(processed_local_json).forEach(item => {
//check, process local data, and extract urls
functions_to_check_and_process_local_data(item);
//processed_local_json['item'];
//define data processing function, that waits on first_api_fetcher (which waits on second api fetcher)
const processData = async() => {
await first_api_fetcher(url1, url2).then(
//select whichever data is best and write to string literals
//join string literals into an array
//return array of string literals
) //end then 
} //end processData
//print string template literals to file (waits on processData)
const printFile = async() => {
const printData = await processData();
//print to file
fs.appendFile("file", printData, (err) => {
if (err) {
console.log(err);
} else {
console.log('wrote strings template literals to file');
}
}); //end write output to file
} //end printfile
//call function to trigger api requests, data processing, and string template literal composition
printFile();
}); //end Object.keys processing loop
} //end try/catch
}) //end readfile

代码并不完美,但除了一个主要缺陷,即对象。Keys循环不会等待API的获取,它会完成工作。

在处理函数中有一个。then()只是为了明确(对我来说)处理发生在api获取之后,然而变量在循环外的作用域内初始化,因此似乎没有必要从api_fetchers

返回值数组有没有人知道如何让Object.keys循环在每次迭代结束时等待api结果?是否有一些布尔值设置发送编译器回循环的开始,直到它与返回的api数据切换?

还是我在文档中遗漏了一些技巧?

附注:在对象中。键循环你定义了一些异步函数。我不建议在每个循环中定义它们!它们应该在循环之外设置。

至于"让循环等待",重要的是要从概念上理解,你并没有让循环本身等待……关键行代码是同步代码。也许你已经知道了,但只是想说清楚。

当您调用该循环中的异步代码(printfile函数)时,您将触发多个异步printfile函数,除非您等待异步函数调用本身。这样就可以强制循环停止进一步的处理,直到每个调用完成。

最新更新