I'm trying to solve a JavaScript challenge where I have to use asynchronous callbacks.
Here is what the challenge says:
Define a function named doStuffAsync that takes one argument callback. Your function should read the contents of file "passwords", write the result to file "world.txt" along with the extra text "OWNED", then call callback with no arguments. Use both the asynchronous readAsync and asynchronous writeAsync.
My code is as follows:
var files = { "passwords": "abc,def",
"world.txt": "hello" };
var readAsync = function(file, callback) {
callback(files[file]);
};
var writeAsync = function (file, contents, callback) {
files[file] = contents;
callback();
};
var test = function() {
files["out.txt"] = "final";
files["passwords"] = "blank";
};
//this is the part I'm interested in
var doStuffAsync = function (callback) {
var contents = 0;
contents = readAsync("passwords");
writeAsync("world.txt", contents, callback());
};
Here is the link to the challenge http://nathansjslessons.appspot.com/lesson?id=1085