Promise是一个非常轻量级且易于使用的对象,允许您异步运行代码,并在需要或希望时随时从任何位置获取结果。Promise是线程安全的。
Promise设计用来立即运行一块代码。您可以通过多种方式使用.run()来启动Promise。例如:
Promise.run {
// here goes some asynchronous code.
}
如果您想要将来在某个地方使用这个Promise,例如获取结果,这可能会是这样的:
let myPromise = Promise.run {
//some method that returns 3
return 3
}
...
// somewhere in the future
var myPromiseResult = 0
if myPromise.isCompleted {
myPromiseResult = myPromise.result as! Int
}
您也可以为Promise提供一个错误处理器
Promise.run(task: {() throws -> Void in
// some meaningful code that might throw an error
}, errorHandler: {(error) -> Void in
// your own error handler
})
如果您希望在代码块内部使用Swift的错误处理方式(如使用do…catch),上面的错误处理可能不是很有用。但是,在下一节中它将显示出强大的功能。
作为Promise灵活性的重要部分,这里有任务的魔法连接。它们可以相互依赖。您可以通过使用.then()来连接它们。
Promise.run {
// some important task that returns "lala lones"
return "lala lones"
}.then {(previousPromiseResult: Any?) -> Void in
// previousPromiseResult contains the value "lala lones" that came from the previous Promise.
// you can extract it by many Swift ways, for example:
let importantString = previousPromiseResult as! String
...
// some important use for the importantString
}.then {
// notice that the previous Promise doesn't return anything. As so, this block doesn't receive
// any parameter. In fact, you can also use the previousPromiseResult overload, however the
// value passed to this parameter is an object of type Void. You can't do anything meaningful
// with Void.
}
您也可以在任何Promise链路点处理错误。所有的块都是可抛出的,因此您可以使用try或抛出自定义异常。
Promise.run {
// some meaningful code
}.then {
try someMeaningfulMethod() //let's say this method threw an error
}.then {
// some meaningful code. This code won't be executed because the
// previous 'Promise' threw an error.
}.then({
// some meaningful code. This code won't be executed because the
// previous 'Promise' threw an error.
}, errorHandler: { (error) throws -> Void in
// the 'error' parameter contains the error thrown at the second Promise.
// notice that this block is also throwable, which means that you if you don't want
// to make this the end, you can rethrow the error, or forward another error if you will.
// the error that this method throws will continue down the chain in the same way
// that .then() does.
// let's say that the error was fully handled here
}).then {
// as you can see, you can continue the chain even if there was an error handler before.
// this method WILL be executed, because the previous Promise contained the 'errorHandler'
// which fully handled the error.
}.then({
// some meaningful code
}, errorHandler: {(error) -> Void in
// another last error handler.
// if you don't provide an error handler and an error is thrown, the error is
// simply discarded and the following Promises after the error are not executed.
})
最后,您可以并行执行多个Promise。实际上,您实例化的每个Promise都会并发运行,但您可以使用.when()来知道所有期望的Promise何时完成。当然,您也可以有一个errorHandler
。
let promise1 = Promise.run {
// some asynchronous and concurrent task.
}
let promise2 = Promise.run {
// some asynchronous and concurrent task.
return 3 // a dummy value just for representation
}.then {(previousPromiseReturn: Any?) -> Void in
// some asynchronous task, but serial with the previous chained 'Promise'
// notice that variable 'promise2' refers to THIS Promise. Equality opertor
// always to refers to the last promise in the chain.
}
let promise3 = Promise.run {
// another asynchronous and concurrent task.
}
Promise.when([promise1, promise2, promise3], errorHandler: {(error) -> Void in
// some block of code that will handle the error.
})
为了给您更多的控制权,您可以通过查看result
属性来了解Promise已经有一个结果可用了。如果result
为nil,Promise还没有可用的结果。或者,您也可以查看布尔方法isCompleted
和hasErrors
来检查Promise是否仍在运行。如果其中任何一个为真,Promise已经停止,由于Promise不能被重新使用,这个Promise可以丢弃。但是,由于手动使用这三个属性更容易出错,因此不建议使用。
它可以在任何用Swift可编程的操作系统中运行。此库仅适用于Swift编程语言。
Promise通过CocoaPods提供。要安装它,只需将以下行添加到Podfile中
pod "Promise"
Movinpixel,[邮箱地址删除]欢迎任何建议!请发送一个Pull Request,或者您可以将建议通过电子邮件发送至[邮箱地址删除]
Promise在本项目下遵循BSD授权协议。更多信息请查看LICENSE文件。