1

I use the following code and the code is not getting to the if statement

function getContent(key) {
    var filePath = path.join(__dirname, '../test.txt');
    fs.readFile(filePath, 'utf8', function (err, data) {
        if (err) {
            return console.log(err);
        }
        ....
        var fileKeyValObj = {};
        return fileKeyValObj[key];
    });
}

I was able to access to the return of the previous method via debug and its working ok... Here the function is not getting to the if

getContent('web', function (cmd) {
    if (typeof cmd !== 'undefined') {

Here the key is the web which I sent and I see that the return of the getContent is with the right value.

I see this post but I think that I did the same ,do I miss something here? How do I return the response from an asynchronous call?

Btw in this case its recommended to use promise??? like blue bird

Community
  • 1
  • 1
  • Returning data from an async callback is not doing what you want. That's just returning back into the bowels of the file system code, not returning to any of your code. You can't try to make an async operation suddenly be synchronous. You just can't. `getContent()` needs to either return a promise or accept a callback that you can call when the result is available. – jfriend00 Jul 31 '15 at 05:49

1 Answers1

0

The trouble is the return value in your callback for fs.readfile is not the effective return value for its containing function getContent.

When you pass a callback to fs.readFile, the function you pass in is queued to run when fs.readFile completes. In simple terms, getContent does the enqueuing and then says "My job's finished!" and returns (void), but fs.readfile is waiting for itself to complete before running the callback.

When fs.readfile runs its callback, it is out of the context of getContent. It will return, but "nobody will be there" to pick up the return value.

What you can do instead is pass in a callback to getContent which is then passed to fs.readFile.

function getContent(key, callback) {
    var filePath = path.join(__dirname, '../test.txt');
    fs.readFile(filePath, 'utf8', callback);
}

getContent('web', function (error, data) {
    if (!error) {
        ... //Put your code here!
    } else {
        console.log(error);
    }  
});

Now, getContent will run its second argument function (error, data) when fs.readFile completes.

heartyporridge
  • 1,151
  • 8
  • 25
  • Thanks, but after the readfile there is additional logic which I should use ,where should I put it? –  Jul 30 '15 at 18:31
  • Ok so what should I put in the callback in this case? –  Jul 30 '15 at 18:48
  • @JhonDree: You should place the `callback` call where you currently have placed your `return` statement. – Bergi Jul 30 '15 at 18:50
  • @Bergi- Little bit confused can you please provide example? if you can help with promise its lot better ... –  Jul 30 '15 at 18:52
  • When I call `getContent`, I pass in two arguments: `web`, and `function (error, data)...`. That second argument becomes `callback` in the `getContent` function arguments. We then pass that same function into `fs.readFile`'s second argument, `callback`. – heartyporridge Jul 30 '15 at 18:58
  • @analytalica - I understood that you pass two params as error and data but confused regard the callback ,can you edit your answer with what should I need to change please? –  Jul 30 '15 at 19:04
  • No, I mean, the `function` in the call to `getContent` *is* the `callback` argument. – heartyporridge Jul 30 '15 at 19:05
  • So this is not the same of what have I tried already in my post?instead of callback I use inline function... –  Jul 30 '15 at 19:07
  • The difference is now you can access the data inside `//Put your code here!` which you could not do before. – heartyporridge Jul 30 '15 at 19:29