Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/raigorx/promises-js
https://github.com/raigorx/promises-js
async await educational example promises setinterval settimeout tutorial
Last synced: 8 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/raigorx/promises-js
- Owner: raigorx
- License: gpl-3.0
- Created: 2024-06-09T10:24:21.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-06-10T00:47:23.000Z (5 months ago)
- Last Synced: 2024-06-10T11:49:34.551Z (5 months ago)
- Topics: async, await, educational, example, promises, setinterval, settimeout, tutorial
- Language: JavaScript
- Homepage:
- Size: 20.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](https://raigorx.github.io/Promises-JS/)
### 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.