Instantly share code, notes, and snippets.

Embed
What would you like to do?
ES6 Promise Examples
/**
* @date 2015/3/21
* @author Dolphin<dolphin.w.e@gmail.com>
*
* Callback 风格的文件合并
*/
var fs = require('fs');
function getFilesInDir(dir, cb) {
fs.readdir(dir, function (err, files) {
if (err) {
cb(err);
return;
}
var jsFiles = files.filter(function (filename) {
return filename.match(/\.js$/);
}).map(function (filename) {
return dir + '/' + filename;
});
cb(null, jsFiles);
});
};
function readFiles(files, cb) {
files = [].slice.call(files);
var results = [],
done = false,
remain = files.length;
files.forEach(function (path, index) {
console.log('read file ' + path);
fs.readFile(path, 'utf-8', function (err, data) {
if (err) {
done = true;
cb(err);
return;
}
results[index] = data;
if (!done && !--remain) {
done = true;
cb(null, results);
}
});
});
};
function output(options, contentList, cb) {
var outputPath = options.output,
banner = options.banner,
separator = options.separator + '\n' || '\n';
console.log('write file ' + outputPath);
var content = contentList.join(separator);
content = banner + '\n' + content;
fs.writeFile(outputPath, content, 'utf-8', function (err) {
if (err) {
cb(err);
}
});
};
function concatJS(options, cb) {
if (!options) {
return cb(new Error('concat options is required'));
}
var writeFile = output.bind(null, options);
getFilesInDir(options.path, function(err, files) {
if (err) {
return cb(err);
}
readFiles(files, function (err, contentList) {
if (err) {
return cb(err);
}
writeFile(contentList, function (err) {
if (err) {
return cb(err);
}
});
});
});
};
module.exports = concatJS;
/**
* @date 2015/3/21
* @author Dolphin<dolphin.w.e@gmail.com>
*
* Promise 风格的文件合并
*/
var fs = require('fs');
function getFilesInDir(dir) {
return new Promise(function (resolve, reject) {
fs.readdir(dir, function (err, files) {
if (err) {
reject(err);
return;
}
var jsFiles = files.filter(function (filename) {
return filename.match(/\.js$/);
}).map(function (filename) {
return dir + '/' + filename;
});
resolve(jsFiles);
});
});
};
function readFiles(files) {
var promises = files.map(function (path) {
return new Promise(function (resolve, reject) {
console.log('read file ' + path);
fs.readFile(path, 'utf-8', function (err, data) {
if (err) {
return reject(err);
}
resolve(data);
});
});
});
return Promise.all(promises);
};
function output(options, contentList) {
var outputPath = options.output,
banner = options.banner,
separator = options.separator + '\n' || '\n';
console.log('write file ' + outputPath);
return new Promise(function (resolve, reject) {
var content = contentList.join(separator);
content = banner + '\n' + content;
fs.writeFile(outputPath, content, 'utf-8', function (err) {
if (err) {
reject(err);
return;
}
resolve();
});
});
};
function concatJS(options) {
if (!options) {
return Promise.reject(new Error('concat options is required'));
}
var writeFile = output.bind(null, options);
return getFilesInDir(options.path)
.then(readFiles)
.then(writeFile)
.catch(function (err) {
throw new Error(err);
});
};
module.exports = concatJS;
/**
* @date 2015/3/21
* @author Dolphin<dolphin.w.e@gmail.com>
*/
var fs = require('fs'),
concatWithPromise = require('./concat-with-promise'),
concatWithCallbacks = require('./concat-with-callbacks');
function makeFiles() {
'abcdefg'.split('')
.forEach(function (char) {
fs.writeFileSync(
'src/' + char + '.js',
'console.log("this is ' + char + '.js");'
);
});
};
!fs.existsSync('src') && fs.mkdirSync('src'), makeFiles();
!fs.existsSync('dist') && fs.mkdirSync('dist');
console.log('========= Testing Promises =========\n');
concatWithPromise({
path: 'src',
output: 'dist/output-promise.js',
banner: [
'/*',
' * Generated with Promise',
' */'
].join('\n'),
separator: '\n'
}).then(function () {
console.log('\n========= Testing Callbacks =========\n');
concatWithCallbacks({
path: 'src',
output: 'dist/output-callback.js',
banner: [
'/*',
' * Generated with Callbacks',
' */'
].join('\n'),
separator: '\n'
}, function (err) {
console.log(err);
});
}).catch(function (err) {
console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment