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
/*** Event Loop ***/ | |
// More priority for the promises over the setTimeOut and other ajax calls | |
// Promises will be added to a seperate queue having more priority than callback. | |
// Callbacks will be in a different queue in background which will be executed after the promises | |
console.log('STARTS'); | |
setTimeout(() => console.log('0 secs timer.'), 0); | |
Promise.resolve('Promise Resolve 1').then(response => { | |
for(let i = 0; i < 40000000; i++) {} | |
console.log(response) | |
}); | |
Promise.resolve('Promise Resolve 2').then(response => console.log(response)); | |
console.log('ENDS'); | |
// First promise will execute and later setTimeOut due to priority | |
/*** Developing a Promise ***/ | |
const examResultPromise = new Promise(function(resolve, reject) { | |
console.log('Exam evaluation begins.'); | |
setTimeout(function() { | |
if(Math.random() >= 0.5) { | |
resolve('You are passed, Congratulations!!!'); | |
} | |
else { | |
reject(new Error('You are failed, Sorry!!!')); | |
} | |
}, 3000) | |
}); | |
examResultPromise.then(res => console.log(res)).catch(err => console.error(err)) | |
/*** Promisifying setTimeOut ***/ | |
const wait = function(seconds) { | |
return new Promise(function(resolve) { | |
setTimeout(resolve, seconds * 1000); | |
}); | |
}; | |
wait(2).then(() => { | |
console.log('Waited for 2 seconds.') | |
return wait(1); | |
}) | |
.then(() => console.log('Waited for 1 second')); | |
/*** Async - Await instead of promise ***/ | |
const getCountriesInfo = async function(country) { | |
// fetch(`https://restcountries.com/v3.1/name/${country}`).then(res => console.log(res)) | |
const res = await fetch(`https://restcountries.com/v3.1/name/${country}`) | |
console.log(res); | |
const data = await res.json(); | |
console.log(data); | |
return data[0].capital[0]; | |
}; | |
getCountriesInfo('india'); | |
/*** Error handling with try and catch ***/ | |
try { | |
const myConst = 90; | |
myConst = 100; | |
} | |
catch(error) { | |
console.error(`*** ${error.message}`); | |
} | |
/*** Async functions returning a value ***/ | |
//getCountriesInfo('australia') | |
//.then(res => console.log(res)); | |
// without Async, Await cannot be used | |
// Using IIFE | |
(async function() { | |
const testVal = await getCountriesInfo('australia'); | |
console.log(testVal) | |
})(); | |
/*** Calling promises in parallel ***/ | |
const get2Countries = async function(c1, c2) { | |
const country1 = await getCountriesInfo(c1); | |
// 2nd one will be called after the first one | |
const country2 = await getCountriesInfo(c2); | |
console.log(country1, country2); | |
// To call all promises together | |
const countries = await Promise.all([ | |
getCountriesInfo(c1), | |
getCountriesInfo(c2) | |
]); | |
console.log(countries); | |
}; | |
get2Countries('australia', 'canada'); | |
/*** | |
Promise: race - Which is fulfilled first either resolved or rejected | |
***/ | |
// Promise.race for resolve | |
(async function() { | |
const res = await Promise.race([ | |
getCountriesInfo('india'), | |
getCountriesInfo('canada'), | |
getCountriesInfo('usa') | |
]); | |
console.log(res); | |
})(); | |
// Promise.race for reject | |
Promise.race([ | |
getCountriesInfo('india'), | |
wait(0.2) | |
]) | |
.then(() => console.log('Waited for sometime....')) | |
.catch(err => console.log(err)); | |
// Promise.allSettled - Gives response of all the promises | |
Promise.allSettled([ | |
Promise.resolve('Success'), | |
Promise.reject('Error'), | |
Promise.resolve('Success 2') | |
]) | |
.then(res => console.log(res)) | |
.catch(error => console.error(error)); | |
// Promise.all - Gives first rejected promise | |
Promise.all([ | |
Promise.resolve('Success'), | |
Promise.reject('Error'), | |
Promise.resolve('Success 2') | |
]) | |
.then(res => console.log(res)) | |
.catch(error => console.error(error)) | |
// Promise.any - Gives first resolved promise | |
Promise.any([ | |
Promise.resolve('Success'), | |
Promise.reject('Error'), | |
Promise.resolve('Success 2') | |
]) | |
.then(res => console.log(res)) | |
.catch(error => console.error(error)) |
No comments:
Post a Comment