PromiseKit 版本:4.0
Xcode 版本: 8.3.2
我最近开始使用 PromiseKit。
实际上,我正在创建一个轮询 HTTP 请求,该请求返回“已完成”或“未完成”。
我必须在每 1 秒后继续进行 HTTP 调用,持续 5 秒。
我需要实现的是,如果任何 1 个电话给我完成状态,我将返回已完成(“完成”)。但是如果我所有的请求都给了我“notCompleted”的响应,我需要返回reject("notCompleted")
return Promise<T> { fulfilled, reject
let timer1 = Timer.scheduledTimer(withTimeInterval: TimeInterval(1), repeats: true) { timer in
pArr.append(Promise<T> { f, r in
doSomeAsyncTask { T in
if success {
f(T)
fulfilled(T)
timer.invalidate()
} else {
r(ErrorNotCompleted)
}
}
// timeout option
_ = after(interval: TimeInterval(15)).then(execute: { () -> Void in
reject(timeoutForConfirmation)
})
})
}
Timer.scheduledTimer(withTimeInterval: TimeInterval(5), repeats: false) { timer in
timer1.invalidate()
timer.invalidate()
when(resolved: pArr).then { results in
let count = results.filter({ result -> Bool in
return result.boolValue
}).count
if count == 0 {
// TODO: then reject here
reject(ErrorNotCompleted)
}
}.catch { error in
print(error)
}
}
}
timer1.fire()
我怎样才能做到这一点?
有没有更好的方法在 PromiseKit 中编写上述代码。