Page updated Nov 12, 2023

Cancel requests

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

const promise = Storage.get('key'); Storage.cancel(promise, "my message for cancellation"); try { await promise; } catch (error) { console.error(error); // We can confirm the error is thrown by the cancellation here if (Storage.isCancelError(error)) { console.error(error.message); // "my message for cancellation" } }
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

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