2

I have the following function

function fileExists(FileName) {
    var retvar="false";
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {

      fileSystem.root.getFile(FileName, null,
           function() {
              fileEntry.file(function() {
                 alert("Exists");
              },
              function() {

       });
      },
      function () {
          retvar = "true";
          alert(retvar);  //says true
      });
     },null);

    alert(retvar);  //says false
}

here, i have to set the value of retvar as true in the inner nested function. I can't pass that value outside that function. Could any one tell me any idea for passing that value to the parent function?

4
  • Are you using "true" and "false" as Strings for a specific reason? Wouldn't it be better to use true and false directly as booleans? Commented Nov 19, 2013 at 10:39
  • i am using it as string only. I need that as string. Commented Nov 19, 2013 at 10:41
  • what is the sequence of execution check by adding 1 more hardcoded parameter in alert... May be your outer alert is called first. Commented Nov 19, 2013 at 10:48
  • the inner alert is executing first. Just now i checked it by differentiating them by alert("inner value : " + retvar); and alert("outer value : " + retvar); the inner alert is executing first Commented Nov 19, 2013 at 10:58

1 Answer 1

3

Oh! thanks google! for answering me!

I had to initialize the variable before the function!

The answer should be

var retvar="";
function fileExists(FileName) {
    retvar = "false";
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {

      fileSystem.root.getFile(FileName, null,
           function() {
              fileEntry.file(function() {
                 alert("Exists");
              },
              function() {

       });
      },
      function () {
          retvar = "true";
          alert(retvar);  //says true
      });
     },null);

    alert(retvar);  //says same if it is assigned as like in the inner loop
}
Sign up to request clarification or add additional context in comments.

1 Comment

Is this only solution? Since it doesn't sound right from OOP point of view to have global variable for every single nested function which needs to return something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.