Javascript Ajax Interview Questions

What is a Promise?

A promise is an object that may produce a single value some time in the future: either a resolved value, or a reason that it’s not resolved (e.g., a network error occurred). A promise may be in one of 3 possible states: fulfilled, rejected, or pending. Promise users can attach callbacks to handle the fulfilled value or the reason for rejection.

The syntax of promise would be as below:

const promise = new Promise(function(resolve, reject) {
// promise description
})

What are the states of promise?

Promises have three states: 1. Pending: This is an initial state of the Promise before an operation begins 2. Fulfilled: This state indicates that specified operation was completed.

  1. Rejected: This state indicates that the operation did not complete. In this case an error value will be thrown.

What is a callback hell?

Callback Hell is an anti-pattern with multiple nested callbacks which makes code hard to read and debug when dealing with asynchronous logic. The callback hell looks like below,

async1(function(){
async2(function(){
async3(function(){
async4(function(){
....
});
});
});
});