Created
July 7, 2017 18:15
-
-
Save eMahtab/d61430fb142206de6d0ffb21bcd6c15b to your computer and use it in GitHub Desktop.
Yes Array.forEach is synchronous but be careful when you write async code inside forEach function body.
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
var fs = require('fs'); | |
fs.readFile('users.json',function(err,data){ | |
var users = JSON.parse(data).results.slice(0,100); | |
console.log("Users "+users.length); | |
users.forEach(function(user,index){ | |
var prefix = index+1; | |
fs.writeFile('users/user_'+prefix+'.json',JSON.stringify(user),function(err){ | |
if(err){ | |
console.log("Error "+err) | |
} | |
else{ | |
console.log(prefix + " writing done") | |
} | |
}) | |
console.log(prefix + " done") | |
}) | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment