暂停功能执行

时间:2016-10-10 10:32:23

标签: javascript node.js asynchronous async-await

我有一个运行两个异步函数的函数,它从数据库中获取数据,当它完成时,将标志更新为true。 我有另一个函数可以在两个异步函数完成取得结果之前运行,我需要做的是推迟执行此函数直到该标志变为true。 我在谷歌上做了一些搜索,我发现asyncawait可以用来等待异步函数(Promise)的结果,但我不确定它是否对我的情况有所帮助。
一些代码:

self.synchronizeData = function()
{
  var me = this;
  //TODO: add more jobs 
  var jobs = [this.getPostsLastId.bind(this), this.getTrunk.bind(this)];
  Async.parallel(jobs, function(err, result){
    me.lastId = result[0];
    me.trunk = result[1];
    me.ready = true;
  });
}

在这个函数中我需要等待更新标志为真:

self.addPost = function(userId, post){
  //Here I need to check the flag update if it is 
  //true to continue else I need to pause and continue later 
  me.newPosts.push(new Post(++me.lastId, userId, me.data, post));
  //other operations
}

有什么建议吗?提前致谢

0 个答案:

没有答案
相关问题