Javascript
Promises
Q&A
Ge»What is a Promise in JavaScript?
NTR Moose key ra eo ae Ra ere Reroraa ll oRemrellUc
CORe Lae Zaria congo) cel oad Neon Tie Re NoVA ae Re aoe oe
operations more easily and avoid callback hell
function fetchData() {
return new Promise((resolve, reject) => {
Past Omens
const data = { message:
resolve(data) ;
fetchData()
.then((result) => {
onsole. log(result.message) ;
})
-catch((error) => {
CEC lagela (ial
yi;How do you create a Promise in JavaScript?
Nol Rero Re rol Rear MU ale Rta Maca Mtoe el Coe Rae]
argument, a function (executor), which has two parameters: ‘resolve’ and “reject
Nolte] MEN MAAR Ne Lara coal Ce teat ce Soi Kel Mt yrs
MUNI a fel]
const myPromise = new Promise((resolve, reject) => {
can as
TOL
ees
ictal C-Lal col
}
hi
What is the purpose of ~async/await™ in
JavaScript?
The “async/await” syntax is used to simplify working with Promises. It allows you
to write asynchronous code that looks more like synchronous code, making it
easier to read and maintain. The ‘async’ keyword defines a function as
asynchronous, and ‘await’ is used inside such a function to wait for a Promise to
SmeeHow do you define an “async” function in
JavaScript?
Noreen ia Rea Re MUM eh aCMeS Tle CMR sm eRan taco)
declaration. An ‘async’ function always returns a Promise.
Smee, CS eg eee mee
const result = await somePromise;
ae cad
+
What is the difference between ~Promise.all()~
and ~Promise.race()~?
Soak OMe Cle Marsa aK AceH OU Me at Maccctlaeye Keli
Clan ace Ma Recolor olna Mcole-1 OMe CN OlMe aN Rea Noe a Marea oda)
an array to resolve or reject, and it returns the result or error of the first Promise
that settles.
How do you handle errors in “async/await ~?
Nero MU Kem (elm ore aoe TMS Vee Ma Re celd
occurs within the “try” block or any awaited Promise rejects, control is passed to
Takeo lle ANN Roa Staats
async function myAsyncFunction() {
Aiea Se
(eT CTlea
pane 180 Clee ee
}
iWhat is Promise chaining?
aru oan Roe UMN one Maat lke Tareas
operations together using the .then() method. It allows you to perform a series
of asynchronous tasks sequentially.
Uae eee Oe ee
ict 1 ee)
pares eee soe) ee
yi
How can you achieve parallel execution of
gad
You can use “Promise.all()” to execute multiple Promises in parallel. Each Promise
MaRCRRe Lice NAAT aCciary Alar Mole a AMC UNAM neRe Tne elke cy
rN
const promises = [promisel, promise2, promise3] ;
eee ee UM Tae) ee eS ee
aHHow can you cancel a Promise or cleanup
resources when it's no longer needed?
You can use an “AbortController and the ‘abort’ method to cancel a Promise or
cleanup resources associated with it. The Promise can catch the “AbortError and
handle it gracefully
ere ae aCe Te adele atte O
const signal = controller.signal;
const myPromise = new Promise((resolve, reject) => {
signal.addEventListener('abort', () => {
reject(new DOMException('Aborted', ‘AbortError'));
sai
ai
Courage cite tad
Explain the concept of 'Promise.race()' and
provide a use case for it.
Tolan Ngo lel OM oN Mol Mel ole Se oLoa Nel Mela emia Macelaal MaMa Re icon
Re eM aU Roma AUER na roles ore omen MeN So)
set a maximum execution time for an asynchronous operation.
Promise. race([myPromise, timeoutPromise]).then((result) => {
pa eee ec 00a eee
aiHow can you implement retry logic using
ge RTS
caste tee leper (yap)
idler) epee)
[oho ROC lacl if
SOO ae
console. log('Retrying...')
return retryOperation(maxRetries - 1)
cae
COMMENT ®
Add your suggestion in the
commentsSy
Mario Ruci
Sr. Fullstack Software Engineer
@ scan ME