Skip to content

AbortablePromise is a Promise with the abitlity to be aborted

Notifications You must be signed in to change notification settings

FabioVergani/AbortablePromise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

AbortablePromise

AbortablePromise enhances the standard Promise with additional functionality to allow aborting the execution of the promise.

In real-life scenarios, AbortablePromise can be particularly useful when dealing with operations that may take a long time to complete, such as network requests, file I/O, or any other asynchronous operation that can be cancelled.

Example

async function fetchData(url) {
  const controller = new AbortController();
  const promise = fetch(url, controller);
  // controller.abort();
  try {
    const response = await promise;
    const data = await response.json();
    return data;
  } catch (error) {
    if ('AbortError' === error.name) {
      console.log('Fetch request was aborted');
    } else {
      throw error;
    }
  }
}

About

AbortablePromise is a Promise with the abitlity to be aborted

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published