Created
August 11, 2014 09:18
-
-
Save vnys/c92fe04291aee959f6b1 to your computer and use it in GitHub Desktop.
Using promises in gulp (requires node 0.11)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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