I don't like anything I've seen so far...
Module
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()
}
}
Usage
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()
})
Seems like you'd either want to...
1) Make the loading synchronous
2) Make a static create
function on DooDad that returns a promise that resolves to a new instance of a DooDad
3) Make the constructor return a Promise (seems weird) Asynchronous constructor
4) Emit an event when its done loading
5) Leave it how it is
6) ????