Clash Royale CLAN TAG #URR8PPP .everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0; up vote 4 down vote favorite Using javascript functions and the old way of doing things I wrote a piece of code to copy the first file in a directory to a file Test . console.log("Copy First File"); var fs = require('fs'); fs.readdir(".", function(err, files) if (err) console.log(err);process.exit(1); fs.exists(files[0], function(exits) fs.readFile(files[0], function(err, data) if (err) console.log(err);process.exit(1); fs.writeFile('Test', data, function(err) if (err) console.log(err);process.exit(1); console.log("Data Copied"); ); ) ); ); After reading about Promises (and several failed attempts). I managed to convert the above to use Promise and .then() chaining. function getAListOfFiles() return new Promise(function(resolve, reject) fs.readdir(".", function(err, files) if (err)...
<!-- main_leaderboard, all: [728,90][970,90][320,50][468,60] --> JavaScript Array Iteration Methods ❮ Previous Next ❯ Array iteration methods operate on every array item. Array.forEach() The forEach() method Calls a function once for each array element. Example var txt = ""; var numbers = [4, 9, 16, 25]; numbers.forEach(myFunction); function myFunction(value, index, array) { txt = txt + item + "<br>"; } Try it Yourself » Note that the function takes 3 arguments: The item value The item index The array itself Array.forEach() is not supported in Internet Explorer 8 or earlier: Method forEach() Yes 9.0 Yes Yes Yes Array.map() The map() method creates a new array by performing a function on each array element. This example multiplies each array value by 2: Example var numbers1 = [4, 9, 16, 25]; var numbers2 = numbers1.map(myFunction); function m...