私は私がこれまで見てきた何かを好きではない...大きなファイルを非同期でロードする必要があるノードモジュールには、そのパターンが有用であるという共通のパターンがありますか?
モジュール
import lineReader from 'line-reader'
let bigFile = './huge-file.csv'
export default class DooDad {
constructor() {
this.dictionary = new Map()
// Something like this seems like it'd be nice...
// await this.load()
}
load() {
return new Promise((resolve, reject) => {
lineReader.eachLine(bigFile, (line, last) => {
// this.dictionary = The contents of huge-file.csv
})
})
}
doStuff(foo) {
// Uses this.dictionary to do something interesting
// Problem: Unusable without first calling and waiting for this.load()
}
}
使用
import DooDad from '../doodad'
let doodad = new DooDad()
// We have to first call and wait for load() before doodad is useable
doodad.load().then(x => {
doodad.doStuff()
})
は、あなたがしたいと思うのいずれかのように思えるの...
1)ローディングを同期させる
2)スタティックにするコンストラクタは約束を返してください)あのもの
3の新しいインスタンスに解決の約束を返すあのもので関数は、(その読み込み完了時Asynchronous constructor
4)イベントを放ち)奇妙なようだ
5)残しておく方法
6)
良い点。私は同期が好きで、それが意味をなさないとき、私は工場に「これはオブジェクトへのアクセスがないという利点があります」に基づいて販売されています。 – Jason
@Jason - これがあなたの質問に答えるならば、回答の左側にあるチェックマークをクリックすることでそれをコミュニティに示すことができます。また、スタックオーバーフローに関する正しい手順に従うための評判ポイントを得ることができます。 – jfriend00