grunt.registerTask中的异步任务 [英] Asynchronous tasks in grunt.registerTask

查看:157
本文介绍了grunt.registerTask中的异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在grunt.registerTask中调用两个函数,但是必须在第一个函数完成后调用第二个函数.

I need to call two functions within grunt.registerTask, but the second function has to be called after the first function is done.

所以我想知道我们是否可以在grunt.registerTask中使用回调或Promise或其他异步机制.

So I was wondering if we can use callbacks or promises or other asynchronous mechanisms within grunt.registerTask.

(更具体地说,我需要在第一个函数调用中启动karma,并在第二个函数调用中运行karma(以执行初始单元测试).但是,要运行karma,我需要先启动它.那就是我所缺少的.)

(More specifically, I need to launch karma in my first function call, and run karma in the second function call (to execute the initial unit tests). But in order to run karma, I need to launch it first. And that's what I'm missing.)

推荐答案

我有这个:

grunt.registerTask("name_of_task", ["task_a", "task_b", "task_c"]);

"task_a"完成后必须执行"task_b".问题是"task_a"是异步的并立即返回,因此我需要一种方法使"task_a"具有几秒钟的执行时间.

And "task_b" had to be executed after "task_a" was done. The problem is that "task_a" is asynchronous and returns right away, so I needed a way to give "task_a" a few seconds to execute.

解决方案:

grunt.registerTask("name_of_task", ["task_a", "task_b:proxy", "task_c"]);

grunt.registerTask("task_b:proxy", "task_b description", function () {
        var done = this.async();

        setTimeout(function () {
            grunt.task.run("task_b");
            done();
        }, 2000);
    });
};

这篇关于grunt.registerTask中的异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆