admin管理员组

文章数量:1208155

I am trying to use promise to send an ajax request to a php script which checks if a file exist on the server and returns a boolean value.

I have the below code but the fileExists function always return undefined.

How can I wrap the promise in a function and have the function return the promise value?

function fileExists(url) {
    var promise = new Promise(function(resolve, reject) {
        var xhr = new XMLHttpRequest();
        xhr.onload = function() {
            resolve(this.responseText);
        };
        xhr.onerror = reject;
        xhr.open('GET', url);
        xhr.send();
    }); 
    promise.then(function(e) {
        return e;
    });
}

var result = fileExists("url_to_file");

I am trying to use promise to send an ajax request to a php script which checks if a file exist on the server and returns a boolean value.

I have the below code but the fileExists function always return undefined.

How can I wrap the promise in a function and have the function return the promise value?

function fileExists(url) {
    var promise = new Promise(function(resolve, reject) {
        var xhr = new XMLHttpRequest();
        xhr.onload = function() {
            resolve(this.responseText);
        };
        xhr.onerror = reject;
        xhr.open('GET', url);
        xhr.send();
    }); 
    promise.then(function(e) {
        return e;
    });
}

var result = fileExists("url_to_file");
Share Improve this question asked Jun 22, 2017 at 6:32 K HsuehK Hsueh 6202 gold badges10 silver badges20 bronze badges 5
  • 1 no, fileExists returns undefined, because you don't return anything from that function – Jaromanda X Commented Jun 22, 2017 at 6:33
  • change var promise = to return ... remove the promise.then null function, and finally, realise that var result = fileExists("url_to_file"); will mean result is a Promise - if you want to wait for the promise to fulfill you'll need to do result.then(function(result) { ... code to use result ...}) – Jaromanda X Commented Jun 22, 2017 at 6:36
  • jsfiddle.net/rayon_1990/q063hakm – Rayon Commented Jun 22, 2017 at 6:36
  • Thanks Jaromanda X, this is exactly what was causing the undefined return in my fileExists function – K Hsueh Commented Jun 23, 2017 at 2:40
  • Thanks Rayon for the code – K Hsueh Commented Jun 23, 2017 at 2:43
Add a comment  | 

4 Answers 4

Reset to default 9

Try this:

function fileExists(url) {
  return promise = new Promise(function(resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.onload = function() {
      resolve(this.responseText);
    };
    xhr.onerror = reject;
    xhr.open('GET', url);
    xhr.send();
  }); 
}

fileExists("url_to_file").then(text => console.log(text));

Your function returns nothing. If you return the promise you can hold on to the data once it's resolved.

This part of your code:

promise.then(function(e) {
    return e;
});

only returns e to the callback function. You have to handle the result within that callback.

promise.then(function() {
    // handle result;
});

Or might as well return the promise as shown by @Ole.

You should return the promise, because you need to assign your result variable asynchronous.

function fileExists(url) {
  return new Promise(function(resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.onload = function() {
        resolve(this.responseText);
    };
    xhr.onerror = reject;
    xhr.open('GET', url);
    xhr.send();
  });
}

fileExists("url_to_file").then(console.log);

Your function must return a Promise.

function fileExists(url) {
    return new Promise(function (resolve, reject) {
        var xhr = new XMLHttpRequest();
        xhr.onload = function() {
            resolve(this.responseText);
        };
        xhr.onerror = reject();
        xhr.open('GET', url);
        xhr.send();
    });
}

Then just call the function and print the resolved text

fileExists("url_to_file").then(function (text) {
    console.log(text);
});

本文标签: javascriptPromise returning undefinedStack Overflow