Node.js nodejs异步函数如何实现?

Node.js nodejs异步函数如何实现?,node.js,asynchronous,Node.js,Asynchronous,因此,我不知道如何转换一个同步构建但使用异步调用的函数 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(); // ca

因此,我不知道如何转换一个同步构建但使用异步调用的函数

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\u that()
完成后,它会调用
this.save()
。我不想使用
fs.readFileSync()
fs.writeFileSync()
。我想知道如何进行以下操作:

N.do_that().then( this.save() );

好吧,我知道了。在我的N.做那件事;我添加了一个回调,类似这样:

do_that = (callback) => {
  fs.readFile("file", (err, fd) => {
    // do stuff with fd

    callback();
    return;

  });
}
而不是打电话:

N.do_that();
array.push(N);
this.save();
是的


你可以使用第三方的开源工具,它已经为你提供了承诺版本的帮助!一次谷歌搜索显示:

否则,您可以使用来自的提示自己推荐该方法

(或者,使用回调,您已经在自己的答案中发布了回调。)

N.do_that(() => {
  array.push(N);
  this.save();
};