代码
function downs(url){
return new Promise(function(resolve,reject){
uni.downloadFile({
url: url,
success: function(res) {
resolve(res.tempFilePath);
}
})
})
}
var p1 = downs(url_1);
var p2 = downs(url_2)
var p3 = downs(url_3);
//单个用法
p1.then(data=>{
console.log(data)
})
//多个用法
Promise.all([p1,p2,p3]).then(data=>{
console.log(data)
})
本文介绍了一种使用Promise异步编程模式实现文件下载的方法。通过定义一个downs函数,该函数接收URL作为参数并返回一个Promise对象。此对象在文件下载成功时解析为临时文件路径。文章展示了如何使用单个Promise进行文件下载以及如何使用Promise.all同时处理多个文件下载任务。

1111

被折叠的 条评论
为什么被折叠?



