Promise is object,It is eventuall completion or failure of asynchronous operations.
.catch()-to handle errors in promises .finally()-executes if promise is resolved or rejected
The control of code looses if code accessed by third party api's or looses control of callback to main function.
promises and sync await
fulfilled - completes the promise execution pending- in initial stage of promise creation rejected- if promise is rejected
Returns the value in resolve function
By using async await
It takes an array and returns all values of array if all are resolved or if any one is rejected then it gives error
It takes an array and returns an array of all promises if resolved or rejected
when nested callbacks present then horizontally code increases it leads to callback hell
.catch()
It returns value of resolved promise value
fulfilled - completes the promise execution pending- in initial stage of promise creation rejected- if promise is rejected
gets fulfilled with undefined value
promise.race() returns first resolved or rejected value promise.any() returns first resolved value
To avoid callback hell and inversion of control, easy readable format
using promises and async await
creation phase execution phase
first is microtask queue and next is callback queue
promise chaining
resolve and reject
it waits till that promise is completed
promise
try catch
By passing two arguments to the .then method.
difficult to read and code increases horizontally
yes
yes
program continues without waiting ,sometimes to unexcepted behaviour
to get resolve data
Call stack is used to keep track of function call during the execution of code.
processed promises are present,event loop sends them to call stack
pending,fulfilled,reject
it returns the resolved value of promise
async awiat
path,utf-8,callback
JavaScript handles blocking processes by pausing execution until completion and delegates non-blocking tasks to APIs, enabling concurrent execution.
constructor new promise
callbacks is function passed as argument of function
callback hell,inversion of control
no
returns uniqueid of settimeout
it helps to execute the code in finally block of code if promise reject or resolve.
Event loop is used to send promises in microtask queue and callstacks in callbacks to callstack.
settimeout executes after time interval for once whereas setinterval executes continously till we stop
using clearinterval
async is used create the async function to run as asynchronously and await used to pause the execution of code.
asynchronous is non blocking of code and runs parallel whereas synchronous runs line by line sometimes may lead to blocking of code
setinterval id
Task queue contains callback functions of asynchronous operations, event loop sent this one by one to call stack if empty.
Promise is a object, evntually completion or failure of asynchronous operations.
first completes the microtask queue and next goes to task queue.
resolved data it takes.
Promise.race() gives the first completion promise value whether it success or reject.
The catch will handle the reject and rest of chain is stopped.
callback and timeinterval and message.
It takes the http urls and fetch the data and returns response of promise and then by using json we can get content of the file.
It returns value if success or returns error if first promise is rejected.
whatever value is specified in return
async
microtask methods
yes, if async keyword is used before function name
Both to handle async operations where as promises takes one argument as callback and promise avoid callback hell and inversion control, whereas callback is function passed as argument of function
fs.promise not have callback whereas fs has callback
Event loop passes the async operations of microtask queue and callback queue to callstack
Call stack is used to execute when function is called and after execution it is popped out.
It is easy to read and follows synchronous execution.
It is easy to read and to avoid chaining we use async await.
.catch()
yes
by using async await
by using promise chaining
promises
It gives the first resolved promise if it is success or reject
Event loop is used to send promises in microtask queue and callstacks in callbacks to callstack.
implicitly wrapped in a promise.
Wait till that activity completed
calling all promises in an array
To avoid callback hell and inversion of control and to do handle async operations.
promises
till highest set timeout
resolve and reject
10sec
call stack and heap memory
call stack,heap memory and garbage collector
function and object
Used to consume the resolved promise
Highest priority queue
in readable stream
If an async function has 3 await calls with 2, 3, and 5 seconds, respectively, how can you start them simultaneously?
promise.all()
synchronous and single threaded,non blocking asynchronous and executing tasks in event loop.
.then() chaining,.catch() to handle errors
promises
used to chain the multiple async operations sequentially.
yes
one time
promises used .then(),.catch() and async await used async and await keywords and debugging and readble of code is easy in asyncawiat compare to promises
newly microtask queue is executed before next task executed
4methods , promise.all(),promise.allsetteled(),promise.race() and promise.any()
callback hell and inversion of control
promises and async/await
single thread,because of the nature of the environment
when we want multiple promise result
Where does the execution of an immediately invoked async function expression (IIFE) pass: call stack or microtask queue?
The execution of an immediately invoked async function expression (IIFE) passes through the call stack.
new Promise()
setTimeout(()=>{},1000,arg1,arg2...)
callback queue
passing a argument to function
promise.all(),promise.allsettled(),promise.any(),promise.race()
asyn()=>{}
returns buffer data
using .then()
resolve and reject
console(),fetch(),setTimeout()
if all are rejected values in promise.allsettled it gives aggregate error
yes
fs.writefile(path,data,(err))
it do not convert directly into promise
array of success and reject promises
Using promises
What happens when a promise is rejected in the middle of a chain? Does it continue executing further code?
no,it is terminated
```javascript
Promise.resolve('a').then((data)=>{
console.log(data)
setTimeout(()=>{
console.log(('b'))
},1)
})
setTimeout(()=>{
console.log("c")
},0)
Promise.resolve('d').then((data)=>{
console.log(data)
setTimeout(()=>{
console.log(('e'))
},1)
})
```
a d c b e
It executes the statements which are pushed by event loop.
Normal execution happens