如您所見,我有一個 js,它采用 .csv 并為每一行呼叫一個異步函式(4 個不同的迭代函式)。
問題是在進行 i 1 迭代之前,我需要等待第 i 次迭代中的函式結束。
const csv = require('csv-parser');
const fs = require('fs');
var i=1;
fs.createReadStream('table.csv')
.pipe(csv())
.on('data', (row) => {
switch(i%4){
case 1: org1createPatient(row.patientId, row.FirstName, row.LastName, row.Age, row.Sex, row.ChestPainType, row.RestingBP, row.Cholesterol, row.FastingBS, row.RestingECG, row.MaxHR, row.ExerciseAngina, row.Oldpeak, row.ST_Slope, row.HeartDisease); break;
case 2: org2createPatient(row.patientId, row.FirstName, row.LastName, row.Age, row.Sex, row.ChestPainType, row.RestingBP, row.Cholesterol, row.FastingBS, row.RestingECG, row.MaxHR, row.ExerciseAngina, row.Oldpeak, row.ST_Slope, row.HeartDisease); break;
case 3: org3createPatient(row.patientId, row.FirstName, row.LastName, row.Age, row.Sex, row.ChestPainType, row.RestingBP, row.Cholesterol, row.FastingBS, row.RestingECG, row.MaxHR, row.ExerciseAngina, row.Oldpeak, row.ST_Slope, row.HeartDisease); break;
case 0: org4createPatient(row.patientId, row.FirstName, row.LastName, row.Age, row.Sex, row.ChestPainType, row.RestingBP, row.Cholesterol, row.FastingBS, row.RestingECG, row.MaxHR, row.ExerciseAngina, row.Oldpeak, row.ST_Slope, row.HeartDisease); break;
}
i ;
})
.on('end', () => {
console.log('CSV file successfully processed');
});
async function org1createPatient(patientId, FirstName, LastName, Age, Sex, ChestPainType, RestingBP, Cholesterol, FastingBS, RestingECG, MaxHR, ExerciseAngina, Oldpeak, ST_Slope, HeartDisease) {
...
}
async function org2createPatient( patientId, FirstName, LastName, Age, Sex, ChestPainType, RestingBP, Cholesterol, FastingBS, RestingECG, MaxHR, ExerciseAngina, Oldpeak, ST_Slope, HeartDisease) {
...
}
async function org3createPatient( patientId, FirstName, LastName, Age, Sex, ChestPainType, RestingBP, Cholesterol, FastingBS, RestingECG, MaxHR, ExerciseAngina, Oldpeak, ST_Slope, HeartDisease) {
...
}
async function org4createPatient( patientId, FirstName, LastName, Age, Sex, ChestPainType, RestingBP, Cholesterol, FastingBS, RestingECG, MaxHR, ExerciseAngina, Oldpeak, ST_Slope, HeartDisease) {
...
}
我怎樣才能得到我想要的?希望我的問題足夠清楚!
uj5u.com熱心網友回復:
在readStream您使用的是這里是異步的,也就是說.on(event, callback)將觸發每一個新的資料塊被讀取時,獨立于任何的callback觸發。換句話說,callback這里函式的執行不影響這個程序,它會并行運行,每次event接收。
這意味著,如果callback要執行一段異步代碼,您很可能會遇到這樣的情況:在event接收到下一次讀取時,此函式的多個實體可能仍在運行。
注意:這適用于任何事件,包括
'end'事件。
如果使用async/awaiton callbackif 只會使該函式的內部邏輯同步。它仍然不會影響讀取資料的速率。
為了做到這一點,你會希望同時使用async/await上callback(使其內部同步),并callback手動暫停和恢復讀操作并行發生的。
const csv = require('csv-parser');
const fs = require('fs');
let i = 1;
const stream = fs.createReadStream('table.csv').pipe(csv());
stream.on('data', async (row) => {
// pause overall stream until this row is processed
stream.pause();
// process row
switch (i%4){
case 1: await org1createPatient(row.patientId, row.FirstName, row.LastName, row.Age, row.Sex, row.ChestPainType, row.RestingBP, row.Cholesterol, row.FastingBS, row.RestingECG, row.MaxHR, row.ExerciseAngina, row.Oldpeak, row.ST_Slope, row.HeartDisease); break;
case 2: await org2createPatient(row.patientId, row.FirstName, row.LastName, row.Age, row.Sex, row.ChestPainType, row.RestingBP, row.Cholesterol, row.FastingBS, row.RestingECG, row.MaxHR, row.ExerciseAngina, row.Oldpeak, row.ST_Slope, row.HeartDisease); break;
case 3: await org3createPatient(row.patientId, row.FirstName, row.LastName, row.Age, row.Sex, row.ChestPainType, row.RestingBP, row.Cholesterol, row.FastingBS, row.RestingECG, row.MaxHR, row.ExerciseAngina, row.Oldpeak, row.ST_Slope, row.HeartDisease); break;
case 0: await org4createPatient(row.patientId, row.FirstName, row.LastName, row.Age, row.Sex, row.ChestPainType, row.RestingBP, row.Cholesterol, row.FastingBS, row.RestingECG, row.MaxHR, row.ExerciseAngina, row.Oldpeak, row.ST_Slope, row.HeartDisease); break;
}
i ;
// resume overall stream
stream.resume();
});
stream.on('end', () => {
// now guaranteed that no instances of `callback` is still running in parallel when this event is fired
console.log('CSV file successfully processed');
});
uj5u.com熱心網友回復:
下面的解決方案是使用iter-ops庫,在這種情況下它非常有效,因為pipe(csv())回傳一個AsyncIterable,因此應該對其進行相應處理。
由于您不關心這些處理函式回傳什么,我們可以限制每一行的處理:
const {pipe, throttle, onEnd} = require('iter-ops');
const csv = require('csv-parser');
const fs = require('fs');
const asyncIterable = fs.createReadStream('table.csv').pipe(csv());
const i = pipe(
asyncIterable,
throttle(async (row, index) => {
switch (index % 4) {
case 1: await org1createPatient(row.patientId, ...); break;
case 2: await org2createPatient(row.patientId, ...); break;
case 3: await org3createPatient(row.patientId, ...); break;
case 0: await org4createPatient(row.patientId, ...); break;
default: break;
}
}),
onEnd(s => {
console.log(`Finished in ${s.duration}ms`);
})
);
async function processCSV() {
// this will trigger the iteration:
for await(const a of i) {
// iterating through entire CSV
}
}
PS 我是iter-ops的作者。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/387118.html
標籤:javascript 节点.js 循环 异步等待
下一篇:在地圖功能內反應本機導航
