Created
June 6, 2020 14:10
-
-
Save cipolleschi/e3452c6dee8bdc6d7c5ed94fb2cef48e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Promisification of asynchronous code: | |
| func downloadUser() -> Promise<User> { | |
| return Promise<User> { | |
| resolve, reject, cancellation in | |
| self.downloadUser() { | |
| user in | |
| resolve(user) | |
| } | |
| } | |
| } | |
| func downloadFavouriteMovies(for user: User) -> Promise<[Movie]> { | |
| return Promise<[Movie]> { | |
| resolve, reject, cancellation in | |
| self.downloadFavouriteMovies(for: user) { | |
| movies in | |
| resolve(movies) | |
| } | |
| } | |
| } | |
| func downloadRatings(for movies: [Movies) -> Promise<[Rating]> { | |
| return Promise<[Rating]> { | |
| resolve, reject, cancellation in | |
| self.downloadRatings(for: movies) { | |
| ratings in | |
| resolve(ratings) | |
| } | |
| } | |
| } | |
| func downloadFriends(for user: User) -> Promise<[User]> { | |
| return Promise<[User]> { | |
| resolve, reject, cancellation in | |
| self.downloadFriends(for: user) { | |
| friends in | |
| resolve(friends) | |
| } | |
| } | |
| } | |
| func downloadMovies(for friends: [User]) -> Promise<[Movie]> { | |
| return Promise<[Movie]> { | |
| resolve, reject, cancellation in | |
| self.downloadMovies(for: friends) { | |
| movies in | |
| resolve(movies) | |
| } | |
| } | |
| } | |
| // usage: | |
| let user = try await(downloadUser()) | |
| let movies = try await(downloadFavouriteMovies(for: user)) | |
| let ratings = try await(downloadRatings(for: movies)) | |
| let friends = try await(downloadFriends(for: users)) | |
| let friendsMovies = try await(downloadMovies(for: friends)) | |
| self.presentProfile(for: user, movies: movies, ratings: ratings, friends: friends, movies: movies) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment