Last active
June 14, 2022 08:33
-
-
Save idiotWu/39c4a36ef79f65782f68 to your computer and use it in GitHub Desktop.
ES6 Promise Examples
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
/** | |
* @date 2015/3/21 | |
* @author Dolphin | |
* | |
* 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; |
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
/** | |
* @date 2015/3/21 | |
* @author Dolphin | |
* | |
* 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; |
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
/** | |
* @date 2015/3/21 | |
* @author Dolphin | |
*/ | |
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