node.js - gulp.task async issue
某草草
某草草 2017-05-16 13:21:33
0
2
830

How to understand this sentence: Are your tasks running before the dependencies are complete? Make sure your dependency tasks are correctly using the async run hints: take in a callback or return a promise or event stream.

Translation of gulpjs Chinese website: Is your task running before the tasks of these pre-dependencies are completed? Please make sure that the tasks in the task list you depend on use the correct asynchronous execution method: use a callback, or return a promise or stream.

I'm a little confused. If callback is not used or promise/stream is returned, then this task is not an asynchronous task?

There is also gulp.task('mytask',['beforemytask']), which means that beforemytask is executed before mytask is executed, which is a bit synchronous, but beforemytask needs to use the correct asynchronous method?

Confused, please ask God to clarify my doubts

某草草
某草草

reply all(2)
左手右手慢动作

If callback is not used or promise/stream is returned, then this task is not an asynchronous task?

Yes. Gulp task sequence execution relies on Orchestrator, which will determine whether the function has parameters (callback) and return value (promise/stream) to determine whether it is asynchronous.

But beforemytask needs to use the correct asynchronous method?

You need to detect the end of beforemytask and use the above method to ensure that mytask is executed later. Otherwise, any method will do.

小葫芦

You don’t know when this task will be completed. Add a callback processing later. As far as this task is concerned, the processing here is asynchronous.
Synchronization is exactly what you said, one task must wait for another Tasks can only be processed after they are executed. Sometimes there are dependencies between tasks,

It must be handled according to the dependency relationship. I use synchronization to understand it here. I think there is no problem. This is my understanding, for reference only

Here’s an example

var gulp = require('gulp'); 
var minifycss = require('gulp-minify-css'); //压缩css
var uglify = require('gulp-uglify'); //压缩js
var del = require('del'); // 删除目录

gulp.task('clean',function() {
    return del(['dist/**']);
});
//压缩 css 
gulp.task('minify-css',['clean'], function() {
    return  gulp.src('./public/**/*.css')
            .pipe(minifycss())
            .pipe(gulp.dest('./dist'));
});

//压缩 js 
gulp.task('minify-js',['clean'], function() {
    return gulp.src('./public/**/*.js')
            .pipe(uglify())
            .pipe(gulp.dest('./dist'));
});
// 每一次跑任务 需要删除 dist 目录, 然后开始跑  minify-css 和 minify-js 
//执行任务 这里 minify-css 和 minify-js 就依赖前面 clean 
// 也就是说必须等  clean 这个任务跑完,minify-css 和 minify-js 才能执行
// 
// default 是默认执行的任务
gulp.task('default',[
    'clean','minify-css','minify-js'
]);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template