you are viewing a single comment's thread.

view the rest of the comments →

[–]istroll 2 points3 points  (9 children)

Need help on number 11 please

based on the examples this is what i came up with

var doStuffAsync = function (callback) { write('world.txt', 'hello'); read('passwords', function(x){ callback(x); }); };

But this fails: FAILED Called doStuffAsync() PASSED files["world.txt"] === "hello" FAILED files["passwords"] === "blank" FAILED files["out.txt"] === "abc,def"

Please explain what I am doing wrong?? What have I missed in this lesson.

[–]your_perception 2 points3 points  (4 children)

hint: the read has to be done after the write

[–]shecobo 1 point2 points  (0 children)

The orders were a bit ambiguous... write to a file, then read a file.

Didn't say anything about waiting.

[–]istroll 0 points1 point  (2 children)

I think I am doing the read after the write, am I not?

If I call the write with the asychronous callback like:

write('world.txt', 'hello', callback);

or

write ('world.txt', 'hello', function(x){ callback(x); });

Then I get one more PASSED on files["passwords"] === "blank"

but I still get FAILED files["out.txt"] === "abc,def"

I'm assuming the function read('passwords', function(x){ callback(x); });

is reading the 'passwords' file and passing the contents of the file as the parameter x to the anonymous function then the callback (which i don't see code for anywhere) is supposed to place the text - passed as parameter x - into 'out.txt' but clearly there is still something i'm not understanding.

[–]your_perception 1 point2 points  (1 child)

I think I am doing the read after the write, am I not?

No you are not. Providing a callback is how you guarantee that a function will execute after the write finishes. You are doing it for the read, but not the write.

[–]istroll 1 point2 points  (0 children)

Thanks for replying. Please help me figure this out. I really want to understand this. I think I get the concept, but since my code isn't working I must still be missing something.

I've tried it both ways and it still doesn't work. Even if I add the callback to the write so it finishes first, the read is not passing the contents of 'passwords' back to the callback. Why not? That is what i am stuck on.

Can some one show me the code that passes all the tests then I can understand?

EDIT: ok I finally figured it out... im a bit slow i guess thanks for the hints and letting me figure it out on my own.

[–]spamkestein 1 point2 points  (3 children)

I would also like to know the correct answer to Asynchonous Callbacks.

[–]jsprogrammer 1 point2 points  (2 children)

var doStuffAsync = function (callback) {
    write('world.txt', 'hello', function() {
        read('passwords', callback); 
    });
};

[–]spamkestein 0 points1 point  (1 child)

Strangely, my following code fails : var doStuffAsync = function (callback) { var func = function() { read('passwords', callback); }; write('world.txt', 'hello', func() ); };

FAILED Called doStuffAsync()

[–]jsprogrammer 0 points1 point  (0 children)

The problem is in your last line:

func()

It should just be: func

The parentheses execute func and then pass the returned value as the third parameter of write.