Swift 当使用闭包/解析器初始化承诺时,我们可以直接抛出吗?

Swift 当使用闭包/解析器初始化承诺时,我们可以直接抛出吗?,swift,promisekit,Swift,Promisekit,PromiseKit具有此初始值设定项,用于承诺: /// Initialize a new promise that can be resolved with the provided `Resolver`. public init(resolver body: (PromiseKit.Resolver<T>) throws -> Void) 或者我必须像这样使用do/catch: private func getCNContacts() -> Promise<

PromiseKit具有此初始值设定项,用于
承诺

/// Initialize a new promise that can be resolved with the provided `Resolver`.
public init(resolver body: (PromiseKit.Resolver<T>) throws -> Void)
或者我必须像这样使用
do/catch

private func getCNContacts() -> Promise<[CNContact]> {
    return Promise { seal in
      let fullNameKeyDescriptor = CNContactFormatter.descriptorForRequiredKeys(for: .fullName)
      guard let keysToFetch = [fullNameKeyDescriptor, CNContactPhoneNumbersKey] as? [CNKeyDescriptor] else {
        seal.reject(CKPersistenceError.failedToFetch("Could not cast as [CNKeyDescriptor]"))
        return
      }

      do {
        var results: [CNContact] = []
        let fetchRequest = CNContactFetchRequest(keysToFetch: keysToFetch)
        try contactStore.enumerateContacts(with: fetchRequest) { contact, _ in
          results.append(contact)
        }
        seal.fulfill(results)
      } catch {
        seal.reject(error)
      }
    }
  }
private func getCNContacts()->Promise{
返回承诺{盖章
让fullNameKeyDescriptor=CNContactFormatter.descriptorForRequiredKeys(用于:.fullName)
guard let keystefetch=[fullNameKeyDescriptor,CNContactPhoneNumberKey]作为?[CNKeyDescriptor]其他{
seal.reject(CKPersistenceError.failedToFetch(“无法转换为[CNKeyDescriptor]”)
返回
}
做{
var结果:[CNContact]=[]
let fetchRequest=CNContactFetchRequest(keystefetch:keystefetch)
请尝试contactStore.enumerateContacts(使用:fetchRequest){contact,u中的
结果。追加(联系人)
}
完成(结果)
}抓住{
密封。拒绝(错误)
}
}
}
也有这种可能性,但我认为它可能有线程含义:

  private func getCNContacts() -> Promise<[CNContact]> {
    let fullNameKeyDescriptor = CNContactFormatter.descriptorForRequiredKeys(for: .fullName)
    guard let keysToFetch = [fullNameKeyDescriptor, CNContactPhoneNumbersKey] as? [CNKeyDescriptor] else {
      let error = CKPersistenceError.failedToFetch("Could not cast as [CNKeyDescriptor]")
      return Promise(error: error)
    }

    do {
      var results: [CNContact] = []
      let fetchRequest = CNContactFetchRequest(keysToFetch: keysToFetch)
      try contactStore.enumerateContacts(with: fetchRequest) { contact, _ in
        results.append(contact)
      }
      return Promise.value(results)
    } catch {
      return Promise(error: error)
    }
  }
private func getCNContacts()->Promise{
让fullNameKeyDescriptor=CNContactFormatter.descriptorForRequiredKeys(用于:.fullName)
guard let keystefetch=[fullNameKeyDescriptor,CNContactPhoneNumberKey]作为?[CNKeyDescriptor]其他{
let error=CKPersistenceError.failedToFetch(“无法转换为[CNKeyDescriptor]”)
退货承诺(错误:error)
}
做{
var结果:[CNContact]=[]
let fetchRequest=CNContactFetchRequest(keystefetch:keystefetch)
请尝试contactStore.enumerateContacts(使用:fetchRequest){contact,u中的
结果。追加(联系人)
}
返回承诺值(结果)
}抓住{
退货承诺(错误:error)
}
}

在Promise中具有抛出功能,而不在Promise解析块中处理错误是完全有效的

但是,如果要处理错误,可以使用
catch(on:flags:,policy:,)

这是我根据你的问题做的一个小实验

    simplePromise =  Promise { resolver in
        let string = try self.getSomeString()
        resolver.resolve(Result.fulfilled(string))
    }

    simplePromise.done { results in
        print(results)
    }.catch { error in
        print("Error occurred: \(error)")
    }
现在,如果我的方法
getSomeString()
抛出,则调用
catch
块中的代码,但如果成功解析,则调用
done
中的代码

您可以通过创建方法
getSomeString
并从那里抛出

enum MyError: Error {
    case sampleError
}

func getSomeString() throws -> String {
    throw MyError.sampleError
}
而且,也可以通过不投掷来尝试

因此,处理抛出函数错误的通常方法似乎是实现
catch
回调

您也不能自己实现catch,代码也不会崩溃。最后,抛出函数被视为密封的。拒绝


顺便说一下,如果你愿意,你可以看看代码。您不必使用
do{}catch
处理错误,但是PromiseKit在内部处理错误,请查找您的参考。

在PromiseKit内具有抛出功能,而不在Promise解析块内处理错误是完全有效的

但是,如果要处理错误,可以使用
catch(on:flags:,policy:,)

这是我根据你的问题做的一个小实验

    simplePromise =  Promise { resolver in
        let string = try self.getSomeString()
        resolver.resolve(Result.fulfilled(string))
    }

    simplePromise.done { results in
        print(results)
    }.catch { error in
        print("Error occurred: \(error)")
    }
现在,如果我的方法
getSomeString()
抛出,则调用
catch
块中的代码,但如果成功解析,则调用
done
中的代码

您可以通过创建方法
getSomeString
并从那里抛出

enum MyError: Error {
    case sampleError
}

func getSomeString() throws -> String {
    throw MyError.sampleError
}
而且,也可以通过不投掷来尝试

因此,处理抛出函数错误的通常方法似乎是实现
catch
回调

您也不能自己实现catch,代码也不会崩溃。最后,抛出函数被视为密封的。拒绝

顺便说一下,如果你愿意,你可以看看代码。您不必使用
do{}catch
处理错误,但是PromiseKit会在内部执行此操作,请查找您的参考