我如何强制大量呼叫同步运行?

我希望下面的呼叫电话一个接一个地同步运行。 但是他们不遵循命令。

运行序列节点模块在这里没有帮助,因为我没有尝试运行一系列的gulp.task("mytask", ["foo", "bar", "baz"]任务(即它的语法类似于gulp.task("mytask", ["foo", "bar", "baz"]等),而是如下所示,而是连续地吞下”呼叫“。

 gulp.task("dostuff", function (callback) { gulp .src("...") .pipe(gulp.dest("..."); gulp .src("...") .pipe(gulp.dest("..."); gulp .src("...") .pipe(gulp.dest("..."); callback(); }); 

我如何让他们一个接一个跑?

您可以使用asynchronous作为您的调用的控制stream,让他们只有一个任务,也避免你得到一个“金字塔效应”。 所以像这样的东西应该对你的使用情况有好处:

 var async = require('async'); gulp.task('yeah', function (cb) { async.series([ function (next) { gulp.src('...') .pipe(gulp.dest('...') .on('end', next); }, function (next) { gulp.src('...') .pipe(gulp.dest('...') .on('end', next); }, function (next) { gulp.src('...') .pipe(gulp.dest('...') .on('end', next); } ], cb); }); 

这也可以让你有一些error handling和更好地发生问题的地方。

那么,这只是stream,所以你可以听结束事件(注意厄运的金字塔!)

 gulp.task("dostuff", function (callback) { gulp .src("...") .pipe(gulp.dest("...")) .on('end', function () { gulp .src("...") .pipe(gulp.dest("...")) .on('end', function () { gulp .src("...") .pipe(gulp.dest("...")) .on('end', callback); }); }); }); 

但是将其分解成多个任务可能是一个更好的模式,每个任务都依赖于前一个任务。

运行序列:

按照指定的顺序运行一系列gulp任务。 此函数旨在解决您定义运行顺序的情况,但不select或不能使用依赖关系。

npm install –save-dev运行顺序

 // runSequence will ensure this task will run the following tasks in the listed order gulp.task('things-to-do', callback => runSequence( 'clean-up-workspace', 'copy-new-files', 'lint', 'minify', 'do-laundry', 'cook-dinner', 'bath-cat', callback )); 

https://www.npmjs.com/package/run-sequence

对glob使用同步模式

然后返回gulp.src的结果:

 gulp.task('render', function() { var appJsFiles = _.map(glob.sync('src/**/*.js'), function(f) { return f.slice(6); }); // Render the file. return gulp.src('src/template.html') .pipe(template({ scripts: appJsFiles, styles: ['style1.css', 'style2.css', 'style3.css'] })) .pipe(gulp.dest(config.build_dir)); }); 

你可以在最后一个pipe道后添加这样的东西

 .pipe(gulp.dest(FINAL_DEST)) .on('end', () => gulp.src(['build']) .pipe(clean()))