如何解决定义嵌套对象属性之前的承诺解决
我试图返回一个具有两个属性的对象……一个正在生成blob,并且需要时间来解决。但是,JavaScript似乎会在解决诺言之前跳到下一个函数(或“链中的链接”)。有什么办法可以让它等待此嵌套属性解析后再跳到下一个函数?
fetch(prefix + '/download',{
method: 'POST',.....
})
.then((response) => {
var contentDisposition = response.headers.get('content-disposition');
var fileName = contentDisposition.split('filename=')[1].split(';')[0];
return { blob: response.blob(),filename: fileName }
})
.then((blob) => {
return { href: URL.createObjectURL(blob.blob),filename: blob.filename }
})
我收到Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided.
,大概是因为blob.blob返回了Promise并且还没有值。
解决方法
尝试使用自己的response.blob()
返回then()
的诺言,以返回最终数据
fetch(prefix + '/download',{
method: 'POST',.....
})
.then((response) => {
var contentDisposition = response.headers.get('content-disposition');
var filename = contentDisposition.split('filename=')[1].split(';')[0];
return response.blob().then(blob =>{
return { href: URL.createObjectURL(blob),filename}
});
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。