我要将文件放入Firebase存储中,我浏览了一张图像列表,并希望将它们一张一张地放在Firebase中。但是Map并不希望完成图像的上传,它会同时启动所有图像,我希望它一张一张。
createProject() {
let url;
let prevState = this.state;
let project = {
projectName: prevState.projectName,
description: prevState.description,
images: [],
}
let getUrl = function (url) {
console.log('DONE' + url);
}
prevState.project.images.map(async (image, key) => {
let url = await this.putFile(image);
getUrl(url);
})
}
putFile(file, getUrl) {
return new Promise(function (resolve, reject) {
let storageRef = fire.storage().ref('Projects/Images/');
const image = file;
const name = file.imageName;
const metadata = {
contentType: file.data.type,
};
const task = storageRef.child(name).put(image.data, metadata);
task.on('state_changed', function (snapshot) {
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED: // or 'paused'
console.log('Upload is paused');
break;
case firebase.storage.TaskState.RUNNING: // or 'running'
console.log('Upload is running');
break;
}
}, function (error) {
}, function () {
task.snapshot.ref.getDownloadURL().then(function (downloadURL) {
resolve(downloadURL);
});
});
});
}
我希望Map等待上一个“解决”。
答案 0 :(得分:3)
您可以改用异步for-of
循环:
async createProject() {
// ...
for (let image of prevState.project.images) {
let url = await this.putFile(image);
getUrl(url);
}
}