我有一个需要传入2个回调函数的函数,我想将其转换为async/await。但是如何在不断返回进度的同时等待呢?
我正在使用https://github.com/yannickl/AwaitKit来摆脱回调函数。
typealias GetResultCallBack = (String) -> Void
typealias ProgressCallBack = (Double) -> Void
func getFileFromS3(onComplete callBack: @escaping GetResultCallBack,
progress progressCallback: @escaping ProgressCallBack) {
}
我是这样使用它的:
getFileFromS3() { [weak self] (result) in
guard let self = self else { return }
// Do something with result
} progress: { [weak self] (progress) in
guard let self = self else { return }
DispatchQueue.main.async { [weak self] in
guard let self = self else {return}
// Update progress in UI
}
}
以下是没有进度报告的转换代码:
func getFileFromS3() -> Promise<String> {
return async {
// return here
}
}