-1

I'm trying to define a funciton to check if a file exists, but the function always get undefined, even when it finds the file:

  var findFile = function(startURL) {
    var host = location.protocol+'//'+location.hostname+(location.port ? ':'+location.port: '');
    var uri = host + startURL + '/file.js';
    //console.log(uri);
    var http = new XMLHttpRequest();
    http.open('GET',uri,true);
    http.send();   

    var processRequest = function (e) {
      if (http.status == 200) {
        return "found";
      } else {
        console.log(http.status);
        console.log(http.readyState);
        return 'not_found';
      }
    }
    http.onreadystatechange = processRequest;
  }

Running for a valid file (that gets the 200 result from http), I get a undefined result.

a = findFile('/app');
>undefined

What am I doing wrong in return statement?

EDIT

I added the callback function, but I'm still getting undefined:

This is my code now:

  var findServiceWorkerFile = function(startURL,callback) {
    var host = location.protocol+'//'+location.hostname+(location.port ? ':'+location.port: '');
    var uri = host + startURL + '/RoasSDKWorker.js';
    //console.log(uri);
    var http = new XMLHttpRequest();
    http.open('GET',uri,true);
    http.send();   
    var file_status;
    var processRequest = function (e) {
      if (http.status == 200) {
        file_status = 'found';
        callback(file_status);
         "found";
      } else {
        file_status = 'not_found';
        callback(file_status);
      }
    }
    http.onreadystatechange = processRequest;

  }

  function findFileCallBack(value) {
    return value;
  }
Filipe Ferminiano
  • 8,373
  • 25
  • 104
  • 174
  • 4
    Your `findFile()` function doesn't have a `return` statement, only `processRequest()` does. But anyway, given that `findFile()` kicks off an asynchronous Ajax operation it will have returned *before* the results come back. – nnnnnn May 01 '17 at 03:40
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – ibrahim mahrir May 01 '17 at 03:43
  • `findFile` does asynchronous work. – ibrahim mahrir May 01 '17 at 03:43
  • You can use [the function I normally use](https://pastebin.com/eVYtfZaK) if you need a vanilla JS code sample. As for why your code doesn't work, nnnnnn has already explained that. – Pyromonk May 01 '17 at 03:48

1 Answers1

1

You will get undefined as the function findFile has become an async fucntion because of XMLHttpRequest().

The function call a = findFile('/app'); will do the network I/O and return immediately. It wont wait for the network request to complete.

You'll have to use callback to get the return value of findFile function which is a standard javascript paradigm. You can do something like this

var findFile = function(startURL, callback) {
    var host = location.protocol+'//'+location.hostname+(location.port ? ':'+location.port: '');
    var uri = host + startURL + '/file.js';
    //console.log(uri);
    var http = new XMLHttpRequest();
    http.open('GET',uri,true);
    http.send();   

    var processRequest = function (e) {
      if (http.status == 200) {
        callback("found");
      } else {
        console.log(http.status);
        console.log(http.readyState);
        callback("not_found");
      }
    }
    http.onreadystatechange = processRequest;
  }

function b(ret_val) {
  console.log(ret_val);
}
findFile('/app', b);

I haven't tested the code but that is just a hint how you can work on. The code is here

radbrawler
  • 2,391
  • 2
  • 15
  • 22
  • I added the callback, but I'm still getting undefined. Can you check my code please? – Filipe Ferminiano May 01 '17 at 04:03
  • As I said you'll get `undefined` because that part is asynchronously executed, the function returns immediately and adds an event listener to the response of the network I/O as soon as that I/O request is over that event listener is fired again to execute the code. – radbrawler May 01 '17 at 04:53