In my solution I've implemented transaction-like methods of uploading posts media. For example I'm adding new post with photo. I want to have 700x700, 200x200, 70x70, 10x10 px sizes in my storage and original in device album. And when all images are uploaded, then save new post info to database. But this code is not beautiful at all. How can I improve it?// MARK: - Christmas tree// Like transaction :)// Bad view actuallystatic func uploadVideoForPost(with videoURL: URL, for postForUpdate: PostItem,                              screenShot: UIImage,                              completion: @escaping (_ hasFinished: Bool, _ postWithRefs: PostItem?) -> Void) {  var post = postForUpdate // we will insert refs to media to this object  // upload screenshots  upload(screenShot, for: post, withSize: 700.0)  { (hasFinishedSuccessfully, url) in     if hasFinishedSuccessfully {        post.mediaRef700 = url        upload(screenShot, for: post, withSize: 200.0)        { (hasFinishedSuccessfully, url) in           if hasFinishedSuccessfully {              post.mediaRef200 = url              upload(screenShot, for: post, withSize: 70.0)              { (hasFinishedSuccessfully, url) in                 if hasFinishedSuccessfully {                    post.mediaRef70 = url                    upload(screenShot, for: post, withSize: 10.0)                    { (hasFinishedSuccessfully, url) in                       if hasFinishedSuccessfully {                          post.mediaRef10 = url                          // upload video                          upload(with: videoURL, for: post)                          { (hasFinishedSuccessfully, url) in                             if hasFinishedSuccessfully {                                post.videoRef = url                                let path = videoURL.path                                if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path) {                                   UISaveVideoAtPathToSavedPhotosAlbum(path, nil, nil, nil)                                }                                completion(true, post) // after this func i will save post data to db in other func.                             } else {                                completion(false, nil)                             }                          }                       }else {                          completion(false, nil)                       }                    }                 } else {                    completion(false, nil)                 }              }           } else {              completion(false, nil)           }        }     } else {        completion(false, nil)     }   }}Upload func is just a func of uploading with completion handler.
Uploading photos of various sizes
error handling;swift;ios;network file transfer;firebase
You can chain those async methods using Promises.So the code will look like this:fetchUsers().then({ users in    return users[0]}).then({ firstUser in    return fetchFollowers(of: firstUser)}).then({ followers in    self.followers = followers}).catch({ error in    displayError(error)})Take a look at those projects:https://github.com/khanlou/Promisehttps://github.com/mxcl/PromiseKit