Ad

Gulp 4 Multiple Async Subtasks

- 1 answer

how do I run multiple async subtasks in the Gulp task? In general, this task runs and starts copying the files but throws an error at the end.

  • The following tasks did not complete: build, copy:default

  • Did you forget to signal async completion?

gulp.task('copy:default', () => {

  const fonts = gulp.src(['src/fonts/**/*'])
    .pipe(gulp.dest('dist/fonts'));
  const images = gulp.src(['src/images/**/*'])
    .pipe(gulp.dest('dist/images'));
  const scripts = gulp.src(['src/scripts/**/*'])
    .pipe(gulp.dest('dist/scripts'));

  return ['fonts', 'images', 'scripts']

});

I'm using this like this:

gulp.task('build', gulp.series('clean:dist', 'copy:default', 'sass'));
Ad

Answer

Here's a working solution:

gulp.task('copy:default', () => {
  const finalPromise = new Promise(finalResolve => {

    const fonts = new Promise(resolve => {
      gulp.src(['src/fonts/**/*'])
        .pipe(gulp.dest('dist/fonts'))
        .on('end', resolve);
    })

    const images = new Promise(resolve => {
      gulp.src(['src/images/**/*'])
        .pipe(gulp.dest('dist/images'))
        .on('end', resolve)
    })

    const scripts = new Promise(resolve => {
      gulp.src(['src/scripts/**/*'])
        .pipe(gulp.dest('dist/scripts'))
        .on('end', resolve);
    })

    Promise.all([fonts, images, scripts]).then(result => {
      finalResolve(result);
    });
  });

  return finalPromise;
});
Ad
source: stackoverflow.com
Ad