Javascript 吞咽提示回调

Javascript 吞咽提示回调,javascript,node.js,gulp,Javascript,Node.js,Gulp,我似乎不知道如何在任务“2”完成之前等待setTimeout函数完成 问题是'one'是在'two'中我的提示符回调之前执行的 gulp.task('test', ['one']) gulp.task('one', ['two'], function(){ gulputil.log("Starting one"); }); gulp.task('two', function(){ gulputil.log("Starting two") return gulp.src

我似乎不知道如何在任务“2”完成之前等待setTimeout函数完成

问题是'one'是在'two'中我的提示符回调之前执行的

gulp.task('test', ['one'])

gulp.task('one', ['two'], function(){
    gulputil.log("Starting one");
});

gulp.task('two', function(){
    gulputil.log("Starting two")
    return gulp.src('')
        .pipe(prompt.prompt({
            type: 'checkbox',
            name: 'bump',
            message: 'What type of release is it? (Patch: hotfix, Minor: Release, Major: Major release)',
            choices: ['patch', 'minor', 'major']
        }, function(res){
            setTimeout(function(){
                gulputil.log("Starting prompt callback");               
            },200);
        }))
});
我曾尝试将cb添加到函数中,并在提示回调中调用,但它会导致一条关于cb被调用次数过多的错误消息

我发现的唯一修复方法是将提示插件更改为不调用cb,并使用以下命令自己调用cb:

.on('end', function()){
    cb();
});

我是否遗漏了什么,或者提示插件中是否存在错误?

请尝试,因为并行运行的任务和第二个任务不知道第一个任务何时完成。

您是否碰巧调用了回调或cb?如果是这样的话,那就是你的问题-.prompt在内部重用这些函数,并覆盖你的函数,而你的函数需要通过闭包访问setTimeout回调函数。查看来源

这很好:

var gulp = require('gulp');
var prompt = require('gulp-prompt');
var gulputil = require('gulp-util');

gulp.task('test', ['one']);

gulp.task('one', ['two'], function(){
  gulputil.log("Starting one");
});

gulp.task('two', function(f){
 gulputil.log("Starting two");
 function f() { console.log( 'done' ); }
 return gulp.src('test.js')
   .pipe(prompt.prompt({
     type: 'checkbox',
     name: 'bump',
     message: 'What type of release is it? (Patch: hotfix, Minor: Release, Major: Major release)',
     choices: ['patch', 'minor', 'major']
   }, function(res){
              setTimeout(function(res){
                  gulputil.log("Starting prompt callback");
                  f();
               }, 200);               
    }));
})