【问题标题】:Run async task and wait for it运行异步任务并等待它
【发布时间】:2014-12-02 09:51:54
【问题描述】:

如果我执行以下操作:

module.exports = function(grunt) {
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
  });
  grunt.registerTask('myAsync','An async test task',function(){
    var done = this.async();
    setTimeout(function(){
      console.log('This is my async task');
      done();
    },1000);
  });
  grunt.registerTask('myTask','A test task',function(){
    grunt.task.run('myAsync');
    console.log('This is my task');
  });
  grunt.registerTask('default', ['myTask']);
};

输出是:

Running "myTask" task
This is my task

Running "myAsync" task
This is my async task

所以“myTask”没有等待“myAsync”完成。我希望“myTask”等待“myAsync”。想出了以下方法,但不确定是否可以这样做:

module.exports = function(grunt) {
  // Project configuration.
  grunt.myAsync = function myAsync(callback){
    var done = this.async();
    setTimeout(function(){
      console.log('This is my async task');
      callback();
      done();
    },1000);
  };
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
  });
  grunt.registerTask('myAsync','An async test task',function(){
    grunt.myAsync();
  });
  grunt.registerTask('myTask','A test task',function(){
    var done = this.async();
    console.dir(this);
    grunt.myAsync.call(this,function(){
      console.log('This is my task');
      done();
    });
  });
  // Default task(s).
  grunt.registerTask('default', ['myTask']);
};

它允许我让 grunt 运行 'myAsync' 或从另一个任务运行它并等待它。

还有其他方法吗?从任务中调用异步任务时,找不到如何等待它们。

[更新]

Kyle 让我走上了正轨,增加了 3 个任务; 1 删除数据库,2,运行测试,3 运行测试并删除数据库。由于任务在队列中运行,第三个任务只需要运行另外两个。而不是让第一个任务(运行测试)调用第二个任务(drop db)。

看起来像这样:

  grunt.registerTask('runtests','Runs the integration tests.',function(){
    var done = this.async();
    setTimeout(function(){
      console.log('ran the tests db');
      done();
    },100);
  });
  grunt.registerTask('dropdb','Drops the db.',function(){
    var done = this.async();
    setTimeout(function(){
      console.log('droped db');
      done();
    },100);
  });
  grunt.registerTask('dropandrun','Runs the integration tests.',function(){
    if(!grunt.option('nodrop')){
      grunt.task.run('dropsdb');
    }
    grunt.task.run('runtests');
  });

【问题讨论】:

    标签: asynchronous gruntjs


    【解决方案1】:

    Grunt 在队列中运行任务。因此,如果您使用 grunt.task.run() 在另一个任务中排队一个任务,它将在当前任务完成后运行该任务。

    您可以创建自己的函数和库,而不是向grunt 实例本身添加方法,您的任务可以调用这些函数和库。比如这样:

    module.exports = function(grunt) {
      function myAsync(callback){
        setTimeout(function(){
          console.log('This is my async task');
          callback();
        },1000);
      }
      grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
      });
      grunt.registerTask('myAsync','An async test task',function(){
        myAsync(this.async());
      });
      grunt.registerTask('myTask','A test task',function(){
        var done = this.async();
        myAsync(function() {
          console.log('This is my task');
          done();
        });
      });
      grunt.registerTask('default', ['myTask']);
    };
    

    随着这些函数的增长,您可以将它们移动到自己的文件中并像这样使用它们:

    // ./lib/myasync.js
    module.exports = function(callback) {
      setTimeout(function() {
        console.log('This is my async function');
        callback();
      }, 1000);
    };
    

    ...

    // Gruntfile.js
    var myAsync = require('./lib/myasync.js');
    

    【讨论】:

    • 谢谢,我想我这次做对了。更新了我的问题。这样做的目的是在队列中咕噜咕噜地运行任务,因此从 task1 调用 task2 时 task1 首先完成,但在 task3 中运行 task1 和 task2 时 task3 先完成,然后是 task1,然后是 task2。
    猜你喜欢
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 2015-10-06
    相关资源
    最近更新 更多