Skip to content

Simplify forcing a retry in afterResponse hook? #778

@sindresorhus

Description

@sindresorhus

Use-case: #776

Maybe we could make that simpler by allowing to force a retry?

I'm thinking returning a symbol, similar to how we have ky.stop:

const api = ky.extend({
	hooks: {
		afterResponse: [
			async (request, options, response) => {
				if (response.status === 200) {
					const data = await response.clone().json();
					if (data.error?.code === 'RATE_LIMIT') {
						return ky.retry;
					}
				}
			}
		]
	}
});

This would skip retry.shouldRetry, but be observable in hooks.beforeRetry. It would be an internal error like KyForceRetry or something. ky.retry would only do something in afterResponse.

An alternative is to expose an error a user could throw in the afterResponse hook, but I kinda prefer returning rather than throwing for control flow.

Is this something worth solving? And any thoughts on the API?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions