Last active
September 7, 2021 14:02
-
-
Save bkvaiude/16211414cf32d0e47723cf485bfdbc7a to your computer and use it in GitHub Desktop.
JavaScript, I promise that I will stay with you, forever!
This file contains 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
let a = () => new Promise(function (resolve, reject) { | |
setTimeout(() => resolve("pass"), 3000); | |
}).then((res) => { console.log("console outcome promise A after 3 secs"); return "As I promised, I'm here! " + res; }) | |
let b = () => new Promise(function (resolve, reject) { | |
setTimeout(() => resolve("pass"), 5000); | |
}).then((res) => { console.log("console outcome promise B after 5 secs", res); return Promise.resolve("I'm resolved!") }) | |
let c = () => new Promise(function (resolve, reject) { | |
setTimeout(() => reject("fail"), 7000); | |
}).catch((err) => { console.log("console outcome promise C after 7 secs", err); return Promise.reject("I'm rejected!") }) | |
let d = () => new Promise(function (resolve, reject) { | |
setTimeout(() => reject("fail"), 0); | |
}).catch((err) => { console.log("console outcome promise D after 0 sec", err); return Promise.reject("I'm rejected immediately!") }) | |
let execAll = () => Promise.allSettled([a(), b(), c()]).then((res) => { console.log("All promises", res) }) | |
let execAllWithHoldPromises = () => Promise.allSettled(promises).then((res) => { console.log("All promises as input pass by let variable", res) }) | |
// promise will return and executed only function is called | |
console.log(a, | |
execAll, | |
execAllWithHoldPromises | |
) | |
// Can I return the any value apart from Promise.resolve or Promise.reject? | |
a() | |
// Let's confirmed that Promise.allSettled really return after 7 secs, even after promise A completed after 3 secs | |
execAll() | |
// check outcome of `[a(), b(), c()]`, it seems promise get evaluated immediately | |
let promises = [a(), b(), c()]; | |
// check outcome of `let promises = [a(), b(), c()]`, and use it as input for Promise.allSettled [tip: global execution context] | |
promises = [a(), b(), c()]; execAllWithHoldPromises() | |
// check outcome of `let promises = [a(), b(), c(), d()]`, and use it as input for Promise.allSettled [tip: global execution context] | |
promises = [a(), b(), c(), d()]; | |
// after 10 secs, execute the following command | |
execAllWithHoldPromises() | |
// Outcome: you will see the uncaught promise exception first time, then after above command execution error will disappeared | |
// Another example with sleep function | |
let quickNap = (i) => new Promise((resolve, reject) => setTimeout(() => resolve(`Slept for ${i} seconds`), i * 1000)) | |
let users = [1, 3, 5] | |
users.map(quickNap) | |
let quickNap = async (i) => new Promise((resolve, reject) => setTimeout(() => { console.log(`Slept for ${i} seconds`); resolve(`Slept for ${i} seconds`) }, i * 1000)) | |
let users = [1, 7, 5] | |
Promise.allSettled(users.map(await quickNap)).then((res) => console.log(res)) | |
let quickNap = (i) => new Promise(resolve => setTimeout(() => { console.log(`Slept for ${i} seconds`); return resolve(`reSlept for ${i} seconds`) }, i * 1000)) | |
let users = [1, 7, 5] | |
// for vs forEach vs map | |
await users.map(await quickNap); | |
await users.forEach(async i => { console.log(`Seq for ${i} seconds`); await quickNap(i) }); | |
for (let i = 0; i < 3; i++) { await quickNap(users[i]) } | |
// When you don't resolve your promise in the `.then` block, output always returns `undefined` | |
let quickNap = (i) => new Promise((resolve, reject) => setTimeout(() => { console.log(`Slept for ${i} seconds`); resolve(`Slept for ${i} seconds`) }, i * 1000)) | |
.then((r) => console.log(`Slept for ${i} seconds`, r)) | |
let users = [1, 7, 5] | |
Promise.allSettled(users.map(quickNap)).then((res) => console.log("hmm...", res)) | |
// When you don resolve your promise without `.then` block, output always returns resolved data | |
let quickNap = (i) => new Promise((resolve, reject) => setTimeout(() => { console.log(`Slept for ${i} seconds`); resolve(`Slept for ${i} seconds`) }, i * 1000)) | |
let users = [1, 7, 5] | |
Promise.allSettled(users.map(quickNap)).then((res) => console.log("hmm...", res)) | |
// quickest way to check how allSettled work with API, fetch API itself returns the promise, not promise wrapper required | |
// Following example will failed to get outcome | |
quickNap = (i) => new Promise(() => fetch('https://api.thecatapi.com/v1/images/search')) | |
Promise.allSettled(users.map(quickNap)).then((res) => console.log(res)) | |
// following one is working example, but strangely it returns promise object | |
quickNap = (i) => fetch('https://api.thecatapi.com/v1/images/search') | |
Promise.allSettled(users.map(quickNap)).then((res) => console.log(res)) | |
// following one is working example, in order to pull out the i need to resolve another promise that is `response.json()` | |
quickNap = (i) => fetch('https://api.thecatapi.com/v1/images/search') | |
Promise.allSettled(users.map(quickNap)).then(async (res) => console.log("------------", await res[0].value.json())) | |
// following one is working example, to simplify it further checking followng example | |
quickNap = (i) => fetch('https://api.thecatapi.com/v1/images/search').then(response => response.json()) | |
Promise.allSettled(users.map(quickNap)).then(async (res) => console.log("------------", res)) | |
// nested example of `allSettled` where main promise is dependant of following `allSettled` multiple promises | |
quickNap = (i) => fetch('https://api.thecatapi.com/v1/images/search').then(response => response.json()) | |
quickNapAllPromises = () => Promise.allSettled(users.map(quickNap)).then((res) => { console.log("------------", res); return res }) | |
execAll = () => Promise.allSettled([a(), b(), c(), quickNapAllPromises()]).then((res) => { console.log("All promises", res) }) | |
execAll() | |
//Does catch block handles the rejects? | |
try{ | |
await c() | |
} | |
catch(e){ | |
console.log("eeeeeeeee-----------",e) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment