Skip to content

Instantly share code, notes, and snippets.

@vnys
Created August 11, 2014 09:18
Show Gist options
  • Save vnys/c92fe04291aee959f6b1 to your computer and use it in GitHub Desktop.
Save vnys/c92fe04291aee959f6b1 to your computer and use it in GitHub Desktop.
Using promises in gulp (requires node 0.11)
'use strict';
var gulp = require('gulp');
var fs = require('fs');
// Make readfile return a promise
function fs_readFile(file, encoding) {
return new Promise(function(resolve, reject) {
fs.readFile(file, encoding, function(err, data) {
if (err) reject(err)
else resolve(data)
})
})
}
gulp.task('default', function() {
fs_readFile('./path/to/file.json')
.then(JSON.parse)
.then(console.log.bind(console))
.catch(function(error) {
console.log('failed!', error)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment