Skip to content

Instantly share code, notes, and snippets.

@yasaswi-24
Created May 24, 2024 19:00
Show Gist options
  • Save yasaswi-24/db0239aa120e6aabcd5e227ce9bc404e to your computer and use it in GitHub Desktop.
Save yasaswi-24/db0239aa120e6aabcd5e227ce9bc404e to your computer and use it in GitHub Desktop.

Explain a promise in JavaScript.

Promise is object,It is eventuall completion or failure of asynchronous operations.

How do you use .catch and .finally with promises?

.catch()-to handle errors in promises .finally()-executes if promise is resolved or rejected

What is inversion of control (IoC) in callbacks?

The control of code looses if code accessed by third party api's or looses control of callback to main function.

How can we fix inversion of control (IoC)?

promises and sync await

Describe the different states of a promise.

fulfilled - completes the promise execution pending- in initial stage of promise creation rejected- if promise is rejected

What does a promise return when it is resolved?

Returns the value in resolve function

How can you avoid excessive chaining of promises?

By using async await

What is Promise.all?

It takes an array and returns all values of array if all are resolved or if any one is rejected then it gives error

What is Promise.allSettled?

It takes an array and returns an array of all promises if resolved or rejected

What is callback hell?

when nested callbacks present then horizontally code increases it leads to callback hell

How do you handle errors in promises?

.catch()

What happens when a promise is resolved?

It returns value of resolved promise value

What are the states of promises?

fulfilled - completes the promise execution pending- in initial stage of promise creation rejected- if promise is rejected

What happens if a promise doesn't return anything?

gets fulfilled with undefined value

What is the difference between Promise.race and Promise.any?

promise.race() returns first resolved or rejected value promise.any() returns first resolved value

What are the uses of promises?

To avoid callback hell and inversion of control, easy readable format

How can you avoid callback hell?

using promises and async await

What are the phases of JavaScript execution?

creation phase execution phase

What is the priority of the microtask and callback queues?

first is microtask queue and next is callback queue

How do you fix the pyramid of doom (callback hell)?

promise chaining

What are the different parameters in promises?

resolve and reject

What is the use of the await keyword?

it waits till that promise is completed

What does an async function return?

promise

How can you handle errors in async functions?

try catch

How do you handle errors in promises without using .catch?

By passing two arguments to the .then method.

What are the drawbacks of callback hell?

difficult to read and code increases horizontally

Is setTimeout asynchronous?

yes

Can you use Promise.all?

yes

What happens if you don't use await in an async function?

program continues without waiting ,sometimes to unexcepted behaviour

How do you use .then with promises?

to get resolve data

What is the call stack in JavaScript?

Call stack is used to keep track of function call during the execution of code.

What is the microtask queue?

processed promises are present,event loop sends them to call stack

What are the steps involved in a promise's lifecycle?

pending,fulfilled,reject

What happens when a promise is fulfilled?

it returns the resolved value of promise

How can you avoid nesting promises?

async awiat

What are the parameters for fs.readFile?

path,utf-8,callback

How does JavaScript handle blocking and non-blocking processes?

JavaScript handles blocking processes by pausing execution until completion and delegates non-blocking tasks to APIs, enabling concurrent execution.

How do you create a promise?

constructor new promise

What are callbacks in JavaScript?

callbacks is function passed as argument of function

What are the problems associated with using callbacks?

callback hell,inversion of control

Can you use await outside of an async function?

no

What does setTimeout return?

returns uniqueid of settimeout

How do you use .finally with promises?

it helps to execute the code in finally block of code if promise reject or resolve.

What is the event loop in JavaScript?

Event loop is used to send promises in microtask queue and callstacks in callbacks to callstack.

What is the difference between setTimeout and setInterval?

settimeout executes after time interval for once whereas setinterval executes continously till we stop

How do you stop setTimeout and setInterval?

using clearinterval

What is the difference between async and await?

async is used create the async function to run as asynchronously and await used to pause the execution of code.

What is the difference between asynchronous and synchronous code?

asynchronous is non blocking of code and runs parallel whereas synchronous runs line by line sometimes may lead to blocking of code

What does setInterval return?

setinterval id

What is the task queue in JavaScript?

Task queue contains callback functions of asynchronous operations, event loop sent this one by one to call stack if empty.

Define promises in JavaScript.

Promise is a object, evntually completion or failure of asynchronous operations.

What is the priority order of the task queue a ond microtask queue?

first completes the microtask queue and next goes to task queue.

What parameters does .then take?

resolved data it takes.

What is Promise.race?

Promise.race() gives the first completion promise value whether it success or reject.

What happens when a promise is rejected in the middle of a chain?

The catch will handle the reject and rest of chain is stopped.

What are the parameters for setTimeout?

callback and timeinterval and message.

How does the Fetch API work?

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.

What does Promise.race return?

It returns value if success or returns error if first promise is rejected.

What is the return value of a synchronous function?

whatever value is specified in return

How does setTimeout with 0ms delay behave: async or sync?

async

Besides promises, what else goes into the microtask queue?

microtask methods

Can you use await in higher-order functions?

yes, if async keyword is used before function name

What is the difference between promises and callbacks in JavaScript?

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

What is the difference between fs and fs.promises?

fs.promise not have callback whereas fs has callback

How does the event loop work in JavaScript?

Event loop passes the async operations of microtask queue and callback queue to callstack

What is the call stack?

Call stack is used to execute when function is called and after execution it is popped out.

What are the benefits of using async and await?

It is easy to read and follows synchronous execution.

Why do we need async and await if promises are available?

It is easy to read and to avoid chaining we use async await.

How do you handle a rejected promise's return value?

.catch()

Can you pass a new Error object in resolve?

yes

How do you change a piece of code to make it asynchronous?

by using async await

How do you avoid inversion of control using promises?

by using promise chaining

What is stored in the microtask queue?

promises

What is Promise.race?

It gives the first resolved promise if it is success or reject

What is the event loop and concurrency model in JavaScript?

Event loop is used to send promises in microtask queue and callstacks in callbacks to callstack.

If an async function does not return a promise, what does it return?

implicitly wrapped in a promise.

What does await do?

Wait till that activity completed

How do you use Promise.all to get data from all promises?

calling all promises in an array

What are the benefits of using promises?

To avoid callback hell and inversion of control and to do handle async operations.

Which asynchronous function has the highest priority in the event loop?

promises

How long does Promise.all take to resolve if you have multiple promises?

till highest set timeout

What does a promise object contain?

resolve and reject

If you have 5 await calls, each taking 2 seconds, how long will it take to execute?

10sec

What is inside the JavaScript engine?

call stack and heap memory

What are the different parts of the JavaScript engine?

call stack,heap memory and garbage collector

What is the output of console.log(typeof Promise) and console.log(typeof anyPromise)?

function and object

What is .then in promises?

Used to consume the resolved promise

What is process.nextTick and where is it used?

Highest priority queue

How does the Fetch API return data?

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()

What model does JavaScript follow, and how does it work?

synchronous and single threaded,non blocking asynchronous and executing tasks in event loop.

What is the best way to handle promises?

.then() chaining,.catch() to handle errors

Which callbacks are stored in the microtask queue?

promises

What is promise chaining?

used to chain the multiple async operations sequentially.

Can you pass functions inside resolve and reject methods?

yes

How many times will a promise resolve if you call resolve 5 times?

one time

What is the difference between promises and async/await?

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

What happens if a microtask generates another microtask?

newly microtask queue is executed before next task executed

How many methods does promises provide to handle concurrency?

4methods , promise.all(),promise.allsetteled(),promise.race() and promise.any()

What is the issue with callbacks?

callback hell and inversion of control

What is the solution to control callbacks?

promises and async/await

Is JavaScript single-threaded or multi-threaded? Explain.

single thread,because of the nature of the environment

When do we use Promise.all?

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.

What are the key elements to create a promise?

new Promise()

How do you pass multiple arguments to setTimeout?

setTimeout(()=>{},1000,arg1,arg2...)

Where is async code stored?

callback queue

What is the role of a callback?

passing a argument to function

What methods are available in promises?

promise.all(),promise.allsettled(),promise.any(),promise.race()

How can you use async functions as arrow functions?

asyn()=>{}

What happens if you don't pass the encoding utf-8 in fs.readFile?

returns buffer data

How do you get and access the data from fs.readFile?

using .then()

What is the essential parameter required in a promise?

resolve and reject

What are some examples of Web APIs?

console(),fetch(),setTimeout()

What is an AggregateError?

if all are rejected values in promise.allsettled it gives aggregate error

Can you pass a callback as an argument in promises?

yes

What is the syntax for writing a file?

fs.writefile(path,data,(err))

What happens if you don't wrap a non-promise type inside a promise?

it do not convert directly into promise

What is the return type of Promise.allSettled?

array of success and reject promises

How is inversion of control solved using 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

What is the output of the following code?

```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

What is the call stack queue?

It executes the statements which are pushed by event loop.

What happens when you write await two times in a row?

Normal execution happens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment