So in my app the user is able to upload 1 to 3 pictures and when he hits the save button I upload them to firebase.
This is my code so far and I know it is horrible and I am looking for a way to make it efficient.
if (img1) {
uploadImage(img1, 'image/jpeg', 'imageOne', uuid)
.then(() => {
console.log('Image 1 was uploaded succesfully');
}).catch(err => console.log(err));
}
if (img2) {
uploadImage(img2, 'image/jpeg', 'imageTwo', uuid)
.then(() => {
console.log('Image 2 was uploaded succesfully');
}).catch(err => console.log(err));
}
if (img3) {
console.log('there is img3')
uploadImage(img3, 'image/jpeg', 'imageThree', uuid)
.then(() => {
console.log('Image 3 was uploaded succesfully');
}).catch(err => console.log(err));
}
The thing is that I want to redirect the user to the home page when the upload finishes. But it is hard to decide where the redirect-code should go.
I thought about doing nested if statements like so:
if (img1) {
uploadImage(img1, 'image/jpeg', 'imageOne', uuid)
.then(() => {
console.log('Image 1 was uploaded succesfully');
if (img2) {
uploadImage(img2, 'image/jpeg', 'imageTwo', uuid)
.then(() => {
console.log('Image 2 was uploaded succesfully');
}).catch(err => console.log(err));
}
}).catch(err => console.log(err));
}
but what If the user uploaded just img2 and not img1? Then img2 would never be uploaded. How can I improve my code?