Asynchronous processing using Promise
PC | MacBook Air(13-inch,2017) |
PC OS | macOS Catalina(ver 10.15.6) |
IDE | Xcode(ver 12.0.1) |
iPhone | SE(2nd Generation) |
iPhone OS | ver 14.0.1 |
Swift | 5.3 |
① 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
.
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
}
}
}
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-----")
}
}
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>
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-----")
}
}
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>
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-----")
}
}
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
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-----")
}
}
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