Javascript 如何按顺序运行多个gulp函数?

Javascript 如何按顺序运行多个gulp函数?,javascript,gulp,Javascript,Gulp,要注册吞咽任务,我使用以下代码: gulp.task('test-module', function() { return testModule(__dirname); }); 这是testModule功能: export function testModule(modulePath) { let task1 = buildModule(modulePath, true); let task2 = buildTestModule(modulePath); let

要注册吞咽任务,我使用以下代码:

gulp.task('test-module', function() {
    return testModule(__dirname);
});
这是
testModule
功能:

export function testModule(modulePath) {
    let task1 = buildModule(modulePath, true);
    let task2 = buildTestModule(modulePath);
    let task3 = runModuleTests(modulePath);
    return [task1, task2, task1];
}
此代码的问题是在
buildModule(modulePath,true)
buildTestModule(modulePath)
生成文件之前调用
runModuleTests(modulePath)
。因此,当执行
runModuleTests(modulePath)
时,没有用于测试的文件,也没有用于测试的文件

我也试过了

import gulpall from 'gulp-all';

export function testModule(modulePath) {
    return gulpall(
            buildModule(modulePath, true),
            buildTestModule(modulePath),
            runModuleTests(modulePath)
    );
}

但结果是一样的。如何修复它?

在gulp任务方法中,可以使用第二个参数定义任务依赖项,这意味着依赖项在主任务之前运行

例如:

gulp.task('after', ['before1', 'before2', ...], function() {

});

您的函数,尤其是
buildModule
buildTestModule
在它们内部执行异步操作。因此,
runModuleTests
在它们完成之前被调用。我用下面的代码模拟了这种行为:

const gulp = require('gulp');

// gulp.task('test-module', function() {
gulp.task('default', function() {
  return testModule(__dirname);
});

function testModule(modulePath) {
  let task1 = buildModule(modulePath, true);
  let task2 = buildTestModule(modulePath);
  let task3 = runModuleTests(modulePath);
  return [task1, task2, task1];
}

function buildModule (path)  {

  setTimeout(() => {
    console.log("in buildModule, should be step 1");
  }, 2000);
};

function buildTestModule (path)  {

    setTimeout(() => {
      console.log("in buildTestModule, should be step 2");
    }, 2000);
};

function runModuleTests (path)  {

  console.log("in runModuleTests, should be step 3");
};
我在前两个函数中加入了延迟,以显示当前面的函数是异步的时会发生什么。结果是:

in runModuleTests, should be step 3
in buildModule, should be step 1
in buildTestModule, , should be step 2
解决这个问题的一种方法是使用async/await和promissions(如果可以的话)。因此,请尝试以下代码:

gulp.task('test-module', function(done) {
    testModule(__dirname);
    done();
});

// function testModule(modulePath) {

async function testModule(modulePath) {

  let task1 = await buildModule(modulePath, true);
  let task2 = await buildTestModule(modulePath);
  let task3 = await runModuleTests(modulePath);

  return [task1, task2, task1];
}

function buildModule (path)  {
  return new Promise(resolve => {

    setTimeout(() => {
        resolve(console.log("in buildModule, should be step 1"));
    }, 2000);

    // put your functionality here without the setTimeout() call above
  });
};

function buildTestModule (path)  {
  return new Promise(resolve => {

    setTimeout(() => {
      resolve(console.log("in buildTestModule, , should be step 2"));
    }, 2000);

    // put your functionality here without the setTimeout() call above
  });
};

function runModuleTests (path)  {
  return new Promise(resolve => {

   // put your gulp.src pipeline here
   console.log("in runModuleTests, should be step 3");
 });
};
结果:

in buildModule, should be step 1
in buildTestModule, , should be step 2
in runModuleTests, should be step 3

所以,让你的函数返回承诺,然后等待结果。这将保证函数以正确的顺序返回。

据我所知,您的回答完全改变了我问题的逻辑。我不需要创建其他任务。我希望只有一个任务具有多个函数。如果您编写的函数正确,则无需更改逻辑,只需在函数周围添加一个包装器即可创建一个吞咽任务。正如我在前面的评论中所说,您的建议是我不想做的。注意,问题在于
顺序运行多个gulp任务函数
,而不是
顺序运行多个gulp任务
。如果这是相同的,你能说:
让task1=等待新的承诺(resolve=>{buildModule(modulePath,true);})?你的版本对我不起作用-gulp不认为buildModule已完成。如果你发现答案有用且准确,不要忘记将其标记为已接受。谢谢。你说的很有意思,但对我来说不是一个好办法。这就是我等待的原因——也许有人能建议什么更适合我。