G
G
game8022017-04-09 23:02:21
Web development
game802, 2017-04-09 23:02:21

How to give return only after exiting a loop in gulp?

Good afternoon dear experts. There is the following code:

gulp.task('copy-template', function() {
  for(var i = 0; i < directory.length; i++) {
    gulp.src(directory[i] + '**/*.*')
      .pipe(gulp.dest(directory[i] + 'template/'));
  }
  return true;
});

In this case, such code does not want to work. How can I make it return gulr.src() after going through the entire loop?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
F6CF, 2017-04-09
@game802

To do this, you can use the merge2 module:

import * as merge2 from 'merge2';
gulp.task('copy-template', ()=>{
  let tasks=[];
  for(var i = 0; i < directory.length; i++) {
    tasks.push(gulp.src(directory[i] + '**/*.*')
      .pipe(gulp.dest(directory[i] + 'template/')));
  }
  return merge2(tasks);
});

Or use gulp4 and async/await from node v7 in order to independently wait for the asynchronous function to finish executing

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question