[Swift] Asynchronous processing using PromiseKit

to write

Asynchronous processing using Promise

Development environment

PCMacBook Air(13-inch,2017)
PC OSmacOS Catalina(ver 10.15.6)
IDEXcode(ver 12.0.1)
iPhoneSE(2nd Generation)
iPhone OSver 14.0.1
Swift5.3

Prerequisites

① Connect MacBook Air and iPhone with a USB cable (2) Create a project application called Sample on the desktop using Xcode. ③ Sample is in the state where PromiseKit is already installed by using CocoaPods. ④ This time, write the code in ViewController.swift in Sample.

Basic code

ViewController.swift


import UIKit
import PromiseKit

class ViewController: UIViewController {

        override func viewDidLoad() {

            let promise = Promise<String> { value in
                //processing
            }.done { value in
                //Run when successful
            }.catch { error in
                //Run when it fails
            }.finally {
                //Last run
            }
        }
    
}

Code example ①

ViewController.swift


import UIKit
import PromiseKit

class ViewController: UIViewController {

        override func viewDidLoad() {
            print("-----promise from here-----")
            
            let promise = Promise<String> { value in
                //processing
                DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
                    value.fulfill("sample1")
                    print("\(Date()) [1]:\(value)")
                }
                DispatchQueue.main.asyncAfter(deadline: .now() + 4.0) {
                    value.fulfill("sample2")
                    print("\(Date()) [2]:\(value)")
                }
                DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
                    value.fulfill("sample3")
                    print("\(Date()) [3]:\(value)")
                }
                DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
                    value.fulfill("sample4")
                    print("\(Date()) [4]:\(value)")
                }
                DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
                    value.fulfill("sample5")
                    print("\(Date()) [5]:\(value)")
                }
                print("\(Date()) [6]:\(value)")
            }.done { value in
                //Run when successful
                print("\(Date()) [7]:\(value)")
            }.catch { error in
                //Run when it fails
                print("\(Date()) [8]:\(error.localizedDescription)")
            }.finally {
                //Last run
                print("\(Date()) [9]: Ends processing")
            }
            print("-----promise up to here-----")
        }
    
}

Code example ① Execution result

Xcode log


-----promise from here-----
2020-10-22 13:16:28 +0000 [6]:PromiseKit.Resolver<Swift.String>
-----promise up to here-----
2020-10-22 13:16:29 +0000 [5]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:16:29 +0000 [7]:sample5
2020-10-22 13:16:29 +0000 [9]: Ends processing
2020-10-22 13:16:30 +0000 [4]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:16:31 +0000 [3]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:16:32 +0000 [2]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:16:33 +0000 [1]:PromiseKit.Resolver<Swift.String>

Code example ②

ViewController.swift


import UIKit
import PromiseKit

enum SampleError: Error {
    case error1
    case error2
    case error3
}

class ViewController: UIViewController {

        override func viewDidLoad() {
            print("-----promise from here-----")
            
            let promise = Promise<String> { value in
                //processing
                value.reject(SampleError.error1)
                
                DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
                    value.fulfill("sample1")
                    print("\(Date()) [1]:\(value)")
                }
                DispatchQueue.main.asyncAfter(deadline: .now() + 4.0) {
                    value.fulfill("sample2")
                    print("\(Date()) [2]:\(value)")
                }
                DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
                    value.fulfill("sample3")
                    print("\(Date()) [3]:\(value)")
                }
                DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
                    value.fulfill("sample4")
                    print("\(Date()) [4]:\(value)")
                }
                DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
                    value.fulfill("sample5")
                    print("\(Date()) [5]:\(value)")
                }
                print("\(Date()) [6]:\(value)")
            }.done { value in
                //Run when successful
                print("\(Date()) [7]:\(value)")
            }.catch { error in
                //Run when it fails
                print("\(Date()) [8]:\(error.localizedDescription)")
            }.finally {
                //Last run
                print("\(Date()) [9]: Ends processing")
            }
            print("-----promise up to here-----")
        }
    
}

Code example ② Execution result

Xcode log


-----promise from here-----
2020-10-22 13:26:04 +0000 [6]:PromiseKit.Resolver<Swift.String>
-----promise up to here-----
2020-10-22 13:26:04 +0000 [8]:The operation couldn’t be completed. (Sample.SampleError error 0.)
2020-10-22 13:26:04 +0000 [9]: Ends processing
2020-10-22 13:26:05 +0000 [5]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:26:06 +0000 [4]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:26:07 +0000 [3]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:26:08 +0000 [2]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:26:09 +0000 [1]:PromiseKit.Resolver<Swift.String>

Code example ③

ViewController.swift


import UIKit
import PromiseKit

enum SampleError: Error {
    case error1
    case error2
    case error3
}

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        print("-----promise from here-----")
        
        let promise = Promise<String> { value in
            //processing
            value.fulfill("sample1")
            print("\(Date()) [1]:\(value)")
            
            value.fulfill("sample2")
            print("\(Date()) [2]:\(value)")
            
            value.fulfill("sample3")
            print("\(Date()) [3]:\(value)")
            
            value.fulfill("sample4")
            print("\(Date()) [4]:\(value)")
            
            value.fulfill("sample5")
            print("\(Date()) [5]:\(value)")
            
            print("\(Date()) [6]:\(value)")
        }.done { value in
            //Run when successful
            print("\(Date()) [7]:\(value)")
        }.catch { error in
            //Run when it fails
            print("\(Date()) [8]:\(error.localizedDescription)")
        }.finally {
            //Last run
            print("\(Date()) [9]: Ends processing")
        }
        print("-----promise up to here-----")
    }
    
}

Code example ③ Execution result

Xcode log


-----promise from here-----
2020-10-22 13:29:03 +0000 [1]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:29:03 +0000 [2]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:29:03 +0000 [3]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:29:03 +0000 [4]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:29:03 +0000 [5]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:29:03 +0000 [6]:PromiseKit.Resolver<Swift.String>
-----promise up to here-----
2020-10-22 13:29:03 +0000 [7]:sample1
2020-10-22 13:29:03 +0000 [9]: Ends processing

Code example ④

ViewController.swift


import UIKit
import PromiseKit

enum SampleError: Error {
    case error1
    case error2
    case error3
}

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        print("-----promise from here-----")
        
        let promise = Promise<String> { value in
            //processing
            value.reject(SampleError.error1)
            
            value.fulfill("sample1")
            print("\(Date()) [1]:\(value)")
            
            value.fulfill("sample2")
            print("\(Date()) [2]:\(value)")
            
            value.fulfill("sample3")
            print("\(Date()) [3]:\(value)")
            
            value.fulfill("sample4")
            print("\(Date()) [4]:\(value)")
            
            value.fulfill("sample5")
            print("\(Date()) [5]:\(value)")
            
            print("\(Date()) [6]:\(value)")
        }.done { value in
            //Run when successful
            print("\(Date()) [7]:\(value)")
        }.catch { error in
            //Run when it fails
            print("\(Date()) [8]:\(error.localizedDescription)")
        }.finally {
            //Last run
            print("\(Date()) [9]: Ends processing")
        }
        print("-----promise up to here-----")
    }
    
}

Code example ④ Output result

Xcode log


-----promise from here-----
2020-10-22 13:33:08 +0000 [1]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:33:08 +0000 [2]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:33:08 +0000 [3]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:33:08 +0000 [4]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:33:08 +0000 [5]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:33:08 +0000 [6]:PromiseKit.Resolver<Swift.String>
-----promise up to here-----
2020-10-22 13:33:08 +0000 [8]:The operation couldn’t be completed. (Sample.SampleError error 0.)
2020-10-22 13:33:08 +0000 [9]: Ends processing

Recommended Posts

[Swift] Asynchronous processing using PromiseKit
[Swift] Asynchronous processing "GCD"
[Swift] What is asynchronous processing?
Asynchronous processing with Spring Boot using @Async
[Swift] Implement swipe processing
[Swift] Try using Collection View
Spring with Kotorin --6 Asynchronous processing
Data processing using Apache Flink
[Swift] Share data using AppDelegate
Asynchronous processing with Shoryuken + SQS
[Processing] Try using GT Force.
Csv output processing using super-csv
[Swift] Implement UITableView using xib
Try implementing asynchronous processing in Azure
[Swift] "for-in statement" about iterative processing
Implement declarative retry processing using Spring Retry
Implementation of asynchronous processing in Tomcat
Deleting files using recursive processing [Java]
Perform parallel processing using Java's CyclicBarrier