0

I have a strange behaviour using gulp as build tool. I got the following code from another question, which is meant for cleaning up build files before another run:

function clean() {
    var delResult = del(['build/**/*', 'dist/**/*']);
    return delResult.then(del(['build', 'dist']));
}

gulp.task('clean', clean);

Then I have my default task which includes the clean task in the rest of the build:

gulp.task('default', gulp.series('clean', gulp.parallel('doJsStuff, doCssStuff', 'doEvenMoreStuff'));

The problem is that gulp already continues with the build before the two del-operations have completed. As I understand it then will return a promise and gulp should continue when that promise gets fulfilled. Of course the then-promise can not get fulfilled before the first del-promise does. So I guess it should work as expected but doesn't.
The behaviour is the same in gulp 3 and 4.

I am aware that I can do the same stuff using only one del-call:

function clean() {
    return del(['build/**', 'dist/**']);
}

That actually works and is a solution for my problem. Still I would like to understand, why the chained promises don't work. Can anybody tell me?

Fencer
  • 1,026
  • 11
  • 27
  • 2
    `.then()` accepts a function, not a `Promise`. Maybe you meant `return delResult.then(() => del(['build', 'dist']));`? – Patrick Roberts Jul 18 '18 at 14:22
  • Ah, I see, of Course, `del(['build', 'dist'])` will be executed immediately not after the first `del` has finished, because I give the result of `del`to `then` and not the `del`-function itself. This obviously leads to undeterministic behaviour. Thanks! I would gladly accept your comment as the answer. – Fencer Jul 31 '18 at 07:12

0 Answers0