Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Gulp "done" method do?

Tags:

gulp

Just a simple question to clarify what does parameter "done" in a gulp task do?

I understand, this is the callback in task function as shown below.

gulp.task('clean', function(done) {
    // so some stuff
    creategulptask(cleantask(), done);
});

But what is the reason to pass it?

like image 473
Nexus23 Avatar asked Apr 17 '15 08:04

Nexus23


People also ask

What is gulp is used for?

Gulp is a tool that helps you out with several tasks when it comes to web development. It's often used to do front end tasks like: Spinning up a web server. Reloading the browser automatically whenever a file is saved.

How does gulp pipe work?

Gulp plugins are Node Transform Streams that encapsulate common behavior to transform files in a pipeline - often placed between src() and dest() using the . pipe() method. They can change the filename, metadata, or contents of every file that passes through the stream.

What is gulp task runner?

Gulp is a task runner that uses Node. js as a platform. It purely uses the JavaScript code and helps to run front-end tasks and large-scale web applications. Gulp builds system automated tasks like CSS and HTML minification, concatenating library files, and compiling the SASS files.


1 Answers

The gulp documentation specifies something similar to the following:

var gulp = require('gulp');

// Takes in a callback so the engine knows when it'll be done
// This callback is passed in by Gulp - they are not arguments / parameters
// for your task.
gulp.task('one', function(cb) {
    // Do stuff -- async or otherwise
    // If err is not null and not undefined, then this task will stop, 
    // and note that it failed
    cb(err); 
});

// Identifies a dependent task must be complete before this one begins
gulp.task('two', ['one'], function() {
    // Task 'one' is done now, this will now run...
});

gulp.task('default', ['one', 'two']);

The done argument is passed into the callback function you use to define your tasks.

Your task function can "accept a callback" function parameter (often this function parameter is named done). Executing that done function tells Gulp "a hint to tell it when the task is done".

Gulp needs this hint if you want to order a series of tasks that depend on each other, as shown in the above example. (i.e. task two won't begin until task one calls cb()) In essence, it stops tasks from running concurrently if you don't want them to.

You can read more about this here: https://github.com/gulpjs/gulp/blob/master/docs/API.md#async-task-support

like image 51
Seer Avatar answered Sep 19 '22 13:09

Seer