我正在尝试创建一个函数,该函数获取预签名的s3 url(调用1)并放入s3。我唯一能弄清楚的方法就是使用嵌套的诺言,我理解这是一种反模式。
用js / pseudocode编写出来
uploadfile(file){
return new Promise((resolve, reject) => {
axios.get(get-presigned-s3url).then((url) =>{ return axios.put(file)}
})
}
let filePromises = files.forEach(file => uploadfile(file));
promises.all((filePromises) => notifyUpload(filePromises));
我需要从uploadfile函数返回一个诺言,以等待所有诺言得以解决。处理这种情况的正确方法是什么?
由于axios.get
已经返回了Promise,因此您无需使用围绕它构造另一个new Promise
。
files.forEach
无效,因为forEach
return undefined
。请.map
改用,这样您就有了一系列的Promises。
const uploadFile = file => axios.get(url)
.then((url) => { return axios.put(file); });
Promise.all(
files.map(uploadFile)
)
.then(notifyUpload)
.catch(handleErrors);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句