【发布时间】:2021-06-03 08:33:27
【问题描述】:
有一个异步迭代
class Fasta {
//read file line by line and yield a class based on every four lines
constructor(path) {
this.path = path
const filestream = fs.createReadStream(this.path)
if (this.path.match(/(\.fastq)|(\.fq)$/)) {
this.filetype = 'fastq'
this.handle = readline.createInterface({
input: filestream,
crlfDelay: Infinity
})
} else if (this.path.match(/\.gz$/)) {
this.filetype = 'fqgz'
this.handle = readline.createInterface({
input: filestream.pipe(zlib.createGunzip()),
crlfDelay: Infinity
})
}
}
async * [Symbol.asyncIterator]() {
let counter = 0
const rec = {0: '', 1: '', 2: '', 3: ''}
for await (const line of this.handle) {
if (counter < 3) {
rec[counter] = line.trim()
counter +=1
} else if (counter == 3) {
rec[counter] = line.trim()
counter = 0
yield new Dna(rec[0], rec[1], rec[3])
}
}
}
}
我想做这样的事情。
for await (const i of zip(new Fasta(args.filea), new Fasta(args.fileb))) {
// do the work
}
我找到了几个walkarouds here,但它们似乎都基于Array.map()。这样,我需要创建一个数组来承载所有数据。当文件很大时,事情就会出错。
我试过了
async function * zip(fasta1, fasta2) {
for await (const [i,j] of [fasta1, fasta2]) {
yield [i,j]
}
}
但它给了我一个“TypeError: .for is not iterable”。
任何帮助将不胜感激!
【问题讨论】:
-
这个问题的答案可能会对您有所帮助。 stackoverflow.com/questions/4856717/… 请检查一下。
-
@Ghazi360 谢谢。但我需要压缩一个可迭代的类,而不是一个数组。创建一个读取大文件的数组会使应用程序崩溃,所以这些答案似乎对我不起作用。
标签: javascript python es6-generator