javascriptgulpgulp-browser-sync

What does () => {} mean in gulpfile.js


I'm in the learning path of js, and just started using the yeoman generators. And I use gulp for the pre-processing and other stuffs. When I was going through the gulpfile.js, I found this block of code.

gulp.task('serve', ['styles', 'html', 'watch'], () => {
    browserSync({
        notify: false,
        port: 9000,
        server: {
            baseDir: 'app'
        }
    })
});

I can understand that when we execute gulp-serve, it runs styles, html and watch task and opens a development server in the port 9000.

But I don't understand what does this () => means.

Will be very grateful if someone can tell me what does that mean.

Thanks in advance.


Solution

  • They are called Arrow functions

    An arrow function expression has a shorter syntax than a function expression and does not bind its own this, arguments, super, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.

    Mozilla Docs

    In simple words, its a substitute for function(){} except for this context

    This is how your code would look:

    gulp.task('serve', ['styles', 'html', 'watch'], function() {
        browserSync({
            notify: false,
            port: 9000,
            server: {
                baseDir: 'app'
            }
        })
    });
    

    (An anonymous function passed in as third argument)

    It's an ES6 Feature anyways, you can explore more features like this in this link :P