Skip to content

Instantly share code, notes, and snippets.

@AntouanK
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save AntouanK/9216499 to your computer and use it in GitHub Desktop.

Select an option

Save AntouanK/9216499 to your computer and use it in GitHub Desktop.
Promises in gulp.js for exiting after many async(?) tasks
// gulpfile.js section
//
// ...
//
var gulp = require('gulp'),
Q = require('q'),
fs = require('fs'),
brands = ['gitHub', 'joyent', 'fractal'];
// check the available brands folders
gulp.task('checkBrands', function(done, res){
var deferred = Q.defer(),
brandsLeft = brands.length;
brands.forEach(function(brand){
var exists = fs.existsSync(brand);
console.log(brand + ' ' + (exists ? 'found' : 'not found'));
if(exists){
existingBrands.push(brand);
}
brandsLeft -= 1;
if(brandsLeft === 0){
deferred.resolve();
}
});
return deferred.promise;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment