2
我有这样的代码如何使用异步回调响应来返回文件内容?
function gotFile(file){
readAsText(file);
}
function readAsText(file) {
var reader = new FileReader();
reader.onloadend = function() {
var string = evt.target.result;
};
alert(string) // returns null
reader.readAsText(file);
}
显然我想从同步功能异步回调导致那是不可能的。
所以我改变了这样的代码:
function gotFile(file){
readAsText(file,function(str){
return str;
});
}
function readAsText(file,callback) {
var reader = new FileReader();
reader.onloadend = function() {
callback(reader.result);
};
reader.readAsText(file);
alert(callback);
}
返回我的字符串
function(str){
return str;
}
我怎样才能做到这一点?
谢谢
我想把这个字符串值放入一个全局变量中并在其他函数中使用它。做一个警报对我没有帮助 – AshBringer
你的其他函数将如何知道字符串值已准备好?它不会。你需要从你的回调中调用其他函数。 –
真的吗?我不能在这个函数中等待它,并在另一个之后使用它? – AshBringer