Skip to content

Instantly share code, notes, and snippets.

@masakih
Created October 18, 2020 02:02
Show Gist options
  • Select an option

  • Save masakih/ecfd48a06d0c01b4353177a8d209e3f3 to your computer and use it in GitHub Desktop.

Select an option

Save masakih/ecfd48a06d0c01b4353177a8d209e3f3 to your computer and use it in GitHub Desktop.
こうやると非同期で動くけど(当たり前) sinkが発動しない(意味不明) #CodePiece
(0...3)
.map { i -> AnyPublisher<Int, Never> in
Future<Int, Never> { promise in
DispatchQueue.global().async {
print("start", i, "----", Thread.current)
sleep(UInt32([0, 2, 3, 5].randomElement()!))
print("end", i)
promise(.success(i * 2))
}
}
.eraseToAnyPublisher()
}
.reduce(Empty<Int, Never>().eraseToAnyPublisher()) { (p, pp) in
p.merge(with: pp).eraseToAnyPublisher()
}
.sink { (i) in
print(i)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment