Skip to content

Commit

Permalink
await for FetchRequest delegate to handle response
Browse files Browse the repository at this point in the history
Closes [hotwired#884]

Despite the fact that `Visit` and `FrameController` are `FetchRequest`
delegate classes that implement their delegate methods asynchronously on
success ([Visit.requestSucceededWithResponse][] and
[FrameController.requestSucceededWithResponse][]) and on failure
([Visit.requestFailedWithResponse][]) and
[FrameController.requestFailedWithResponse][]), the `async
FetchRequest.receive` method doesn't use the `await` keyword to wait for
those delegates to finish handling their callbacks.

This commit adds those `await` clauses.

[Visit.requestSucceededWithResponse]: https://github.com/hotwired/turbo/blob/c207f5b25758e4a084e8ae42e49712b91cf37114/src/core/drive/visit.js#L297
[FrameController.requestSucceededWithResponse]: https://github.com/hotwired/turbo/blob/c207f5b25758e4a084e8ae42e49712b91cf37114/src/core/frames/frame_controller.js#L210
[Visit.requestFailedWithResponse]: https://github.com/hotwired/turbo/blob/c207f5b25758e4a084e8ae42e49712b91cf37114/src/core/drive/visit.js#L311
[FrameController.requestFailedWithResponse]: https://github.com/hotwired/turbo/blob/c207f5b25758e4a084e8ae42e49712b91cf37114/src/core/frames/frame_controller.js#L215

[hotwired#884]: hotwired#884
  • Loading branch information
seanpdoyle committed Oct 13, 2023
1 parent c207f5b commit 2a7c917
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/http/fetch_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class FetchRequest {
try {
this.delegate.requestStarted(this)
const response = await fetch(this.url.href, fetchOptions)
return await this.receive(response)
return this.receive(response)
} catch (error) {
if (error.name !== "AbortError") {
if (this.#willDelegateErrorHandling(error)) {
Expand All @@ -147,9 +147,9 @@ export class FetchRequest {
if (event.defaultPrevented) {
this.delegate.requestPreventedHandlingResponse(this, fetchResponse)
} else if (fetchResponse.succeeded) {
this.delegate.requestSucceededWithResponse(this, fetchResponse)
await this.delegate.requestSucceededWithResponse(this, fetchResponse)
} else {
this.delegate.requestFailedWithResponse(this, fetchResponse)
await this.delegate.requestFailedWithResponse(this, fetchResponse)
}
return fetchResponse
}
Expand Down

0 comments on commit 2a7c917

Please sign in to comment.