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
|
4 Answers
Reset to default 9Try 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
版权声明:本文标题:javascript - Promise returning undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738755406a2110640.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
fileExists
returns undefined, because you don't return anything from that function – Jaromanda X Commented Jun 22, 2017 at 6:33var promise =
toreturn
... remove thepromise.then
null function, and finally, realise thatvar result = fileExists("url_to_file");
will meanresult
is a Promise - if you want to wait for the promise to fulfill you'll need to doresult.then(function(result) { ... code to use result ...})
– Jaromanda X Commented Jun 22, 2017 at 6:36