Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous tasks in 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.

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

(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.)

like image 992
user11081980 Avatar asked Dec 01 '25 23:12

user11081980


1 Answers

I had this:

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

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.

The solution:

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);
    });
};
like image 123
user11081980 Avatar answered Dec 06 '25 22:12

user11081980



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!