所以我不知道如何轉換我有同步構建但使用異步調用的函數。nodejs的異步函數如何?
do_thing =() => {
var N = new Thing(); //sync
N.do_that(); // calls fs.readFile() and does some stuff with data :async
this.array.push(N); // sync but does not affect anything
this.save(); // calls fs.writeFile() but needs info from N that is not created yet as N.do_that() is not done
}
我不知道如何使它所以當N.do_that()
完成它,然後調用this.save()
。我不想使用fs.readFileSync()
或fs.writeFileSync()
。我想知道如何像這樣:
N.do_that().then(this.save());