Page updated Jan 16, 2024

Cancel requests

You may cancel any put, get, or copy requests made by the Storage API by keeping a reference to the promise returned.

1const promise = Storage.get('key');
2
3Storage.cancel(promise, "my message for cancellation");
4
5try {
6 await promise;
7} catch (error) {
8 console.error(error);
9 // We can confirm the error is thrown by the cancellation here
10 if (Storage.isCancelError(error)) {
11 console.error(error.message); // "my message for cancellation"
12 }
13}

Caveat

Since the cancellation requires original reference of the promise, you need to make sure the return value of the request has not been modified. Usually async function wraps the promise being returned into another promise. For example

1async function makeRequest() {
2 return Storage.get('key');
3}
4const promise = makeRequest();
5
6// This won't cancel the request
7Storage.cancel(promise);