Created
December 21, 2015 07:19
-
-
Save jesseky/42bbc6caac53e315a77b to your computer and use it in GitHub Desktop.
node.js callback after all callbacks excuted
This file contains hidden or 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
#!/usr/bin/env node | |
var FS = require('fs'); | |
var pending = callback => { | |
var count = 0; | |
var returns = {}; | |
console.log("Start cound: %d", count); | |
return key => { // a function done | |
count++; | |
console.log("return1: %d", count); | |
return (error, data) => { // called by readFile | |
count--; | |
returns[key] = data; // or add content length: data.length | |
console.log("return2: [%s] = %d", key, count); | |
if (count === 0) { | |
console.log("Callback Excute."); | |
callback(returns); | |
} | |
}; | |
}; | |
}; | |
var done = pending( fileData => { | |
console.log("done"); | |
console.log(fileData); | |
}); | |
// current directory files for test | |
var fileName = ['get.js', 'pha.js', 'promise.js']; | |
for (var i = 0; i < fileName.length; i++) { | |
FS.readFile(fileName[i], 'UTF-8', done(fileName[i])); | |
} | |
// from http://www.cnblogs.com/onlyfu/p/5062068.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment