raigorx / Promises-JS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Overview

This script demonstrates how to work with promises and setTimeout/setInterval functions in JavaScript, utilizing an AbortController to manage and cancel asynchronous operations. It includes functions to create delays, cancel them, and sequence tasks while handling the complexities that arise from using raw setTimeout and setInterval.

Live demo

Live Demo

Task Functions

  • zero(): Demonstrates cancellation with a 0 delay.
  • one(): Shows the effect of catching errors from abort and continuing normal flow.
  • two(): Explains how setTimeout and setInterval behave when cancelled.
  • three(): Illustrates conditional execution based on shouldSleep.
  • four(): Demonstrates the race condition where sleeps never execute due to quick changes in shouldSleep.
  • five(): Shows how throwing an exception cancels function execution.
  • six(): Demonstrates handling promises and extracting resolved values using both await and .then() with setTimeout.
  • seven(): Demonstrates using try/catch with await and .catch with promises to handle errors.

About

License:GNU General Public License v3.0


Languages

Language:JavaScript 87.1%Language:HTML 7.2%Language:CSS 5.7%