Javascript 如何获得函数返回结果?
我正在开发一个文件读取服务,如下所示:Javascript 如何获得函数返回结果?,javascript,angularjs,cordova,callback,Javascript,Angularjs,Cordova,Callback,我正在开发一个文件读取服务,如下所示: angular.factory('fileService', fileService); function fileService($cordovaFile){ var service = { readFile: readFile }; return service; /////////////// function readFile(path, file){ $cordova
angular.factory('fileService', fileService);
function fileService($cordovaFile){
var service = {
readFile: readFile
};
return service;
///////////////
function readFile(path, file){
$cordovaFile.readAsText(path, file)
.then(function (success) {
console.log("read file success");
console.log(success);
return success;
}, function (error) {
alert("Fail to read file:"+error);
console.log("Fail to read file");
console.log(error);
return false;
});
}
}
var data = fileService.readFile(cordova.file.dataDirectory,filename);
console.log(data) //return undefined
var data = fileService.readFile(cordova.file.dataDirectory,filename);
data.then(function (success) {
// Do whatever you need to do with the result
}, function (error) {
/// Handle errors
});
fileService.readFile(cordova.file.dataDirectory, filename).then(function(data) {
// use data here
});
然后像这样使用它:
angular.factory('fileService', fileService);
function fileService($cordovaFile){
var service = {
readFile: readFile
};
return service;
///////////////
function readFile(path, file){
$cordovaFile.readAsText(path, file)
.then(function (success) {
console.log("read file success");
console.log(success);
return success;
}, function (error) {
alert("Fail to read file:"+error);
console.log("Fail to read file");
console.log(error);
return false;
});
}
}
var data = fileService.readFile(cordova.file.dataDirectory,filename);
console.log(data) //return undefined
var data = fileService.readFile(cordova.file.dataDirectory,filename);
data.then(function (success) {
// Do whatever you need to do with the result
}, function (error) {
/// Handle errors
});
fileService.readFile(cordova.file.dataDirectory, filename).then(function(data) {
// use data here
});
问题是它无法返回数据。如何返回数据?您的问题是实际上没有从readFile函数返回任何结果。您正在从回调函数返回数据,但如果您想到它,结果将返回到函数readFile本身,它将保留在该函数中。您要做的是返回函数readFile的整个结果,然后在使用它的控制器中解析承诺。代码如下:
angular.factory('fileService', fileService);
function fileService($cordovaFile){
var service = {
readFile: readFile
};
return service;
function readFile(path, file){
return $cordovaFile.readAsText(path, file);
}
}
然后你像这样使用它:
angular.factory('fileService', fileService);
function fileService($cordovaFile){
var service = {
readFile: readFile
};
return service;
///////////////
function readFile(path, file){
$cordovaFile.readAsText(path, file)
.then(function (success) {
console.log("read file success");
console.log(success);
return success;
}, function (error) {
alert("Fail to read file:"+error);
console.log("Fail to read file");
console.log(error);
return false;
});
}
}
var data = fileService.readFile(cordova.file.dataDirectory,filename);
console.log(data) //return undefined
var data = fileService.readFile(cordova.file.dataDirectory,filename);
data.then(function (success) {
// Do whatever you need to do with the result
}, function (error) {
/// Handle errors
});
fileService.readFile(cordova.file.dataDirectory, filename).then(function(data) {
// use data here
});
通常,当您使用服务来实现某种使用承诺并返回结果的功能时,您应该始终返回承诺对象,该对象可以在任何需要的地方解析。
我强烈建议您阅读此伟大的解释。您的函数
readFile
不返回任何内容,因此,首先您应该返回承诺:
function readFile(path, file) {
return
$cordovaFile.readAsText(path, file).then(function (success) {
console.log('Read file success');
console.log(success);
return success;
}, function (error) {
alert('Fail to read file: ' + error);
console.log('Fail to read file');
console.log(error);
return false;
});
}
然后,如果你试着用你以前的方式使用它,你就不会再没有定义了,你会得到一个承诺
但是由于它是一个异步方法,您将得到仍然挂起的承诺,并且您可能不希望这样,因为您需要承诺的实现值。所以,你应该这样使用它:
angular.factory('fileService', fileService);
function fileService($cordovaFile){
var service = {
readFile: readFile
};
return service;
///////////////
function readFile(path, file){
$cordovaFile.readAsText(path, file)
.then(function (success) {
console.log("read file success");
console.log(success);
return success;
}, function (error) {
alert("Fail to read file:"+error);
console.log("Fail to read file");
console.log(error);
return false;
});
}
}
var data = fileService.readFile(cordova.file.dataDirectory,filename);
console.log(data) //return undefined
var data = fileService.readFile(cordova.file.dataDirectory,filename);
data.then(function (success) {
// Do whatever you need to do with the result
}, function (error) {
/// Handle errors
});
fileService.readFile(cordova.file.dataDirectory, filename).then(function(data) {
// use data here
});
在
readFile
函数运行之前,您的返回服务似乎会将您从函数中跳出。我也不知道函数调用上的点符号是如何工作的/但这可能只是我的误解…@sheriffderek从我的控制台日志中,它表明它正在执行代码。但是它比较慢,并且在数据变量的执行控制台日志之后。$cordovaFile.readAsText()
方法返回一个承诺,它们是异步的,这意味着您不能假定传递给的函数中的代码。然后,它们上的()方法将立即运行。如果这不合理,请查看承诺是如何起作用的。@GregL感谢您的通知。我现在修好了。