Created
July 22, 2023 22:33
-
-
Save ajaymathur/12b28089a33190af90344d3d1f84c795 to your computer and use it in GitHub Desktop.
Promises in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function simplePromiseAsync(api) { | |
| const res = await fetch(api); | |
| return await res.json(); | |
| } | |
| async function promiseAll() { | |
| return await Promise.all([ | |
| simplePromiseAsync('https://catfact.ninjas/fact'), | |
| simplePromiseAsync('https://www.boredapi.com/api/activity') | |
| ]) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function simplePromiseAsync(api) { | |
| const res = await fetch(api); | |
| return await res.json(); | |
| } | |
| async function promiseAllSettled() { | |
| return await Promise.allSettled([ | |
| simplePromiseAsync('https://catfact.ninjas/fact'), | |
| simplePromiseAsync('https://www.boredapi.com/api/activity') | |
| ]) | |
| } | |
| promiseAllSettled() | |
| .then((res) => console.log({ res })) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Cat facts: https://catfact.ninja/fact | |
| * Activity: https://www.boredapi.com/api/activity | |
| */ | |
| /** | |
| * - The `Promise` class is used for handling asynchronous operations in JavaScript. It represents | |
| * a value that may not be available yet, but will be resolved at some point in the future. | |
| * | |
| * - Promises have three states: `pending`, `fulfilled`, and `rejected`. When a promise is in | |
| * the `pending` state, it means that the operation has not yet completed. When it is in the | |
| * `fulfilled` state, it means that the operation completed successfully and the result is | |
| * available. When it is in the `rejected` state, it means that an error occurred and the result | |
| * is not available. | |
| * | |
| * - To use a promise, you create a new instance of the `Promise` class and pass a function to it | |
| * that takes two arguments: a `resolve` function and a `reject` function. The `resolve` function | |
| * is called when the operation completes successfully, and the `reject` function is called when an | |
| * error occurs. | |
| */ | |
| /** | |
| * Promise class | |
| */ | |
| const fetch = require("isomorphic-unfetch"); | |
| async function simplePromiseAsync() { | |
| const res = await fetch('https://catfact.ninja/fact'); | |
| return await res.json(); | |
| } | |
| function simplePromise() { | |
| return new Promise((resolve, reject) => { | |
| try { | |
| fetch("https://catfact.ninja/fact") | |
| .then((res) => res.json()) | |
| .then((res) => resolve(res)); | |
| } catch (err) { | |
| reject(err); | |
| } | |
| }); | |
| } | |
| console.log("before promise"); | |
| simplePromiseAsync() | |
| .then((res) => console.log({ res })) | |
| .catch((err) => console.log({ err })); | |
| console.log("after promise"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function simplePromiseAsync(api) { | |
| const res = await fetch(api); | |
| return await res.json(); | |
| } | |
| async function promiseRace() { | |
| return await Promise.race([ | |
| simplePromiseAsync('https://catfact.ninja/fact'), | |
| simplePromiseAsync('https://www.boredapi.com/api/activity') | |
| ]) | |
| } | |
| promiseRace() | |
| .then((res) => console.log({ res })) | |
| .catch((err) => console.log({ err })); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment