JavaScript FileReader Onsuccess
我的功能看起来像这样:
async handleFile(e){
var file = e.target.files[0];
var reader = new FileReader();
reader.onload = async function(f){
await somelibrary.load(f.target.result)
await somelibrary.render()
await someotherlibrary.init()
}
await reader.readAsText(file);
}
我需要知道负载和渲染是否有效,至少在没有抛出异常的情况下。所以我的想法是做这样的事情:
async handleFile(e){
var file = e.target.files[0];
var reader = new FileReader();
reader.onload = async function(f){
try {
await somelibrary.load(f.target.result)
await somelibrary.render()
await someotherlibrary.init()
return true
} catch (e) {
console.log(e);
return false
}
}
var rendered = await reader.readAsText(file);
console.log(rendered)
}
但是,这只是记录不确定的。我明白为什么我的功能只等待阅读部分,而不仅仅是在.onload中对内容的实际处理...但是我该如何正确执行此操作?
I have a function that looks like this:
async handleFile(e){
var file = e.target.files[0];
var reader = new FileReader();
reader.onload = async function(f){
await somelibrary.load(f.target.result)
await somelibrary.render()
await someotherlibrary.init()
}
await reader.readAsText(file);
}
I need to know if the load and render has worked, at least if they haven't thrown an exception. So my idea was to do something like this:
async handleFile(e){
var file = e.target.files[0];
var reader = new FileReader();
reader.onload = async function(f){
try {
await somelibrary.load(f.target.result)
await somelibrary.render()
await someotherlibrary.init()
return true
} catch (e) {
console.log(e);
return false
}
}
var rendered = await reader.readAsText(file);
console.log(rendered)
}
But this only logs undefined. I understand why, my function only waits for the reading part and not for the actual handling of the content in the .onload... But how do I do this correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论