@@ -28,10 +28,17 @@ public extension NetworkService {
2828 */
2929 @discardableResult
3030 func request< Result> ( _ resource: Resource < Result > ) async throws -> ( Result , HTTPURLResponse ) {
31- return try await withCheckedThrowingContinuation ( { coninuation in
32- request ( resource: resource, onCompletionWithResponse: {
33- coninuation. resume ( with: $0)
31+ var task : NetworkTask ?
32+ let cancel = { task? . cancel ( ) }
33+ return try await withTaskCancellationHandler ( operation: {
34+ try Task . checkCancellation ( )
35+ return try await withCheckedThrowingContinuation ( { coninuation in
36+ task = request ( resource: resource, onCompletionWithResponse: {
37+ coninuation. resume ( with: $0)
38+ } )
3439 } )
40+ } , onCancel: {
41+ cancel ( )
3542 } )
3643 }
3744
@@ -53,10 +60,17 @@ public extension NetworkService {
5360 */
5461 @discardableResult
5562 func request< Result, E: Error > ( _ resource: ResourceWithError < Result , E > ) async throws -> ( Result , HTTPURLResponse ) {
56- return try await withCheckedThrowingContinuation ( { coninuation in
57- request ( resource: resource, onCompletionWithResponse: {
58- coninuation. resume ( with: $0)
63+ var task : NetworkTask ?
64+ let cancel = { task? . cancel ( ) }
65+ return try await withTaskCancellationHandler ( operation: {
66+ try Task . checkCancellation ( )
67+ return try await withCheckedThrowingContinuation ( { coninuation in
68+ task = request ( resource: resource, onCompletionWithResponse: {
69+ coninuation. resume ( with: $0)
70+ } )
5971 } )
72+ } , onCancel: {
73+ cancel ( )
6074 } )
6175 }
6276
0 commit comments