关于 xcode:如何在 Vapor Swift 中等待多个期货完成? | 珊瑚贝

How to wait for multiple futures to complete in Vapor Swift?


我试图弄清楚如何等待多个期货完成。
我知道如何异步等待他们:
summaryFuture.whenSuccess {} 但这只处理一种情况。我需要等待summaryFuture 和sponsorFuture 完成,然后在发回响应之前同时处理两者。

1
2
3
4
5
6
7
8
9
10
11
  let summaryFuture = client.post(summaryURL) { post in
        post.http.headers.add(name:”authtoken”, value: token)
        }.flatMap(to: SummaryModel.self) { (response) in
            return try response.content.decode(SummaryModel.self)
    }

  let sponsorEnrollerFuture = client.post(sponsporEnroller) { post in
        post.http.headers.add(name:”authtoken”, value: token)
        }.flatMap(to: SponsorEnrollerModel.self) { (response) in
            return try response.content.decode(SponsorEnrollerModel.self)
    }


你可以使用 .and 或 .flatten

对于 .flatten,您的期货应该返回 Void,因此最终结果也将是 Future<Void>。

.and 可以这样使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
let summaryFuture = client.post(summaryURL) { post in
    post.http.headers.add(name:”authtoken”, value: token)
}.flatMap(to: SummaryModel.self) { (response) in
    return try response.content.decode(SummaryModel.self)
}

let sponsorEnrollerFuture = client.post(sponsporEnroller) { post in
    post.http.headers.add(name:”authtoken”, value: token)
}.flatMap(to: SponsorEnrollerModel.self) { (response) in
    return try response.content.decode(SponsorEnrollerModel.self)
}

// Now combine the two futures
return summaryFuture.and(sponsorEnrollerFuture).map { summary, sponsorEnrolled in
    print(“summary: \\(summary)”)
    print(“sponsorEnrolled: \\(sponsorEnrolled)”)
}


好的。我想到了。

要等待多个 Futures 完成,您必须使用 .add。在我的特殊情况下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 let summaryFuture = client.post(summaryURL) { post in
        post.http.headers.add(name:”authtoken”, value: token)
        }.flatMap(to: SummaryModel.self) { (response) in
            return try response.content.decode(SummaryModel.self)
    }

  let sponsorEnrollerFuture = client.post(sponsporEnroller) { post in
        post.http.headers.add(name:”authtoken”, value: token)
        }.flatMap(to: SponsorEnrollerModel.self) { (response) in
            return try response.content.decode(SponsorEnrollerModel.self)
    }

  // Now combine the two futures
 let multipleFutures = summaryFuture.add(sponsorEnrollerFuture)

 // Now I can use
 mutipleFutures.whenSuccess = {(summary, sponsorEnroller) in …}


来源:https://www.codenong.com/55284584/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9614.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?