JavaScript Interview Question: What are the states of a Promise? Answer: A Promise has three states: 1. Pending → initial state 2. Fulfilled → operation completed successfully 3. Rejected → operation failed Example: 𝘧𝘦𝘵𝘤𝘩("/𝘢𝘱𝘪") .𝘵𝘩𝘦𝘯(𝘳𝘦𝘴 => 𝘳𝘦𝘴.𝘫𝘴𝘰𝘯()) .𝘤𝘢𝘵𝘤𝘩(𝘦𝘳𝘳 => 𝘤𝘰𝘯𝘴𝘰𝘭𝘦.𝘦𝘳𝘳𝘰𝘳(𝘦𝘳𝘳)) Explanation: Once a Promise is fulfilled or rejected, its state becomes immutable and cannot change. Follow-up Interview Question: Can a Promise change state after it is fulfilled? Answer: No. Explanation: Once resolved or rejected, a Promise cannot transition to another state. #javascript #promises #AsyncProgramming #SoftwareEngineering
Promise States: Pending, Fulfilled, Rejected
More Relevant Posts
-
JavaScript Interview Question: What is Promise.all()? Answer: Promise.all() runs multiple promises in parallel and resolves when all succeed. Example: 𝘗𝘳𝘰𝘮𝘪𝘴𝘦.𝘢𝘭𝘭([𝘧𝘦𝘵𝘤𝘩𝘜𝘴𝘦𝘳𝘴(), 𝘧𝘦𝘵𝘤𝘩𝘗𝘰𝘴𝘵𝘴()]) .𝘵𝘩𝘦𝘯(([𝘶𝘴𝘦𝘳𝘴, 𝘱𝘰𝘴𝘵𝘴]) => { 𝘤𝘰𝘯𝘴𝘰𝘭𝘦.𝘭𝘰𝘨(𝘶𝘴𝘦𝘳𝘴, 𝘱𝘰𝘴𝘵𝘴) }) Explanation: If any Promise fails, Promise.all() immediately rejects. Follow-up Interview Question: When should you use Promise.all()? Answer: When multiple independent async tasks can run simultaneously. #javascript #promises #AsyncProgramming #FrontendDevelopment
To view or add a comment, sign in
-
JavaScript Interview Question: What is the difference between Promises and Callbacks? Answer: Callbacks pass a function to execute after async operations. Promises represent the result of an async operation. Explanation: Promises provide: 1. better readability 2. error handling 3. chaining support Follow-up Interview Question: Why did callback hell occur? Answer: Because nested callbacks made code difficult to maintain. #javascript #AsyncProgramming #WebDevelopment #SoftwareEngineering
To view or add a comment, sign in
-
JavaScript Interview Question: What is a Promise in JavaScript? Answer: A Promise is an object that represents the eventual completion or failure of an asynchronous operation. A Promise can be in three states: 1. pending 2. fulfilled 3. rejected Example: 𝘤𝘰𝘯𝘴𝘵 𝘱𝘳𝘰𝘮𝘪𝘴𝘦 = 𝘯𝘦𝘸 𝘗𝘳𝘰𝘮𝘪𝘴𝘦((𝘳𝘦𝘴𝘰𝘭𝘷𝘦, 𝘳𝘦𝘫𝘦𝘤𝘵) => { 𝘴𝘦𝘵𝘛𝘪𝘮𝘦𝘰𝘶𝘵(() => 𝘳𝘦𝘴𝘰𝘭𝘷𝘦("𝘋𝘰𝘯𝘦"), 1000) }) Explanation: Promises allow developers to handle asynchronous operations without deeply nested callbacks. They make async code easier to read and manage. Follow-up Interview Question: Why were Promises introduced in JavaScript? Answer: To solve problems like callback hell and better manage asynchronous workflows. #javascript #promises #AsyncProgramming #WebDevelopment
To view or add a comment, sign in
-
JavaScript Interview Question: What is Promise.allSettled()? Answer: Promise.allSettled() waits for all promises to finish regardless of success or failure. Example: 𝘗𝘳𝘰𝘮𝘪𝘴𝘦.𝘢𝘭𝘭𝘚𝘦𝘵𝘵𝘭𝘦𝘥([𝘱𝘳𝘰𝘮𝘪𝘴𝘦1, 𝘱𝘳𝘰𝘮𝘪𝘴𝘦2]) Explanation: It returns an array of objects describing each promise result. Follow-up Interview Question: When should you use Promise.allSettled() instead of Promise.all()? Answer: When you need results from all promises even if some fail. #javascript #promises #AsyncProgramming #WebDevelopment
To view or add a comment, sign in
-
JavaScript Interview Question I Faced Recently 💻 One of the questions I forgot to share earlier: Problem: Convert the array "["a","1","b","2","c","3"]" into the object "{ a: 1, b: 2, c: 3 }" My Approach: const arr = ["a","1","b","2","c","3"]; const result = {}; for (let i = 0; i < arr.length; i += 2) { result[arr[i]] = arr[i + 1]; } console.log(result); Idea: The array contains alternating key–value pairs. So we iterate through the array by 2 steps and assign: - "arr[i]" → key - "arr[i+1]" → value Simple but a good way to test JavaScript fundamentals and array manipulation. Have you seen similar questions in interviews? 🤔 #JavaScript #FrontendDevelopment #CodingInterview #WebDevelopment #Learning
To view or add a comment, sign in
-
JavaScript Interview Question: What is Promise.race()? Answer: Promise.race() returns the result of the first promise that settles (resolved or rejected). Example: 𝘗𝘳𝘰𝘮𝘪𝘴𝘦.𝘳𝘢𝘤𝘦([𝘢𝘱𝘪𝘊𝘢𝘭𝘭(), 𝘵𝘪𝘮𝘦𝘰𝘶𝘵()]) Explanation: It is useful when implementing timeouts for API requests. Follow-up Interview Question: What happens if the first Promise rejects? Answer: The entire Promise.race() rejects immediately. #javascript #promises #AsyncProgramming #SoftwareEngineering
To view or add a comment, sign in
-
JavaScript Interview Question: How do you handle errors in Promises? Answer: Using .catch(). Example: 𝘧𝘦𝘵𝘤𝘩("/𝘢𝘱𝘪") .𝘵𝘩𝘦𝘯(𝘳𝘦𝘴 => 𝘳𝘦𝘴.𝘫𝘴𝘰𝘯()) .𝘤𝘢𝘵𝘤𝘩(𝘦𝘳𝘳 => 𝘤𝘰𝘯𝘴𝘰𝘭𝘦.𝘦𝘳𝘳𝘰𝘳(𝘦𝘳𝘳)) Explanation: .catch() handles errors from any previous promise in the chain. Follow-up Interview Question: Can errors inside .then() be caught by .catch()? Answer: Yes. Explanation: Errors thrown inside .then() propagate to .catch(). #javascript #promises #ErrorHandling #WebDevelopment
To view or add a comment, sign in
-
🚨 JavaScript Interview Question (Advanced 🔥) What will be the output? 🤔 function Person(name) { this.name = name; } Person.prototype.getName = function () { return this.name; }; const p1 = new Person("Suman"); const p2 = { name: "Rahul", }; console.log(p1.getName()); console.log(p1.getName.call(p2)); Looks simple… but there’s a twist 👀 👉 What will be the output? 👉 Why does the second call behave differently? Bonus: What concept is being tested here? 🔥 #JavaScript #FrontendInterview #Prototypes #ThisKeyword #WebDevelopment
To view or add a comment, sign in
-
🚨 JavaScript Interview Question What will be the output? 🤔 function greet(name) { if (name === undefined) { console.log("Hello, guest!"); } else { console.log("Hello, " + name); } } greet(); greet("Amrutha"); greet("Vineeth", "How are you?"); Looks simple… but there’s a twist 👀 👉 What will be the output? 👉 Why does the last call behave differently? Bonus: How does JavaScript handle extra arguments? 🔥 #JavaScript #FrontendInterview #WebDevelopment #CodingInterview #ProductBasedCompany
To view or add a comment, sign in
-
JavaScript Interview Question I faced recently 💻 Convert this array "["a","1","b","2","c","3"]" into this object "{ a: 1, b: 2, c: 3 }" const arr = ["a","1","b","2","c","3"]; const obj = {}; for (let i = 0; i < arr.length; i += 2) { obj[arr[i]] = arr[i + 1]; } console.log(obj); // { a: "1", b: "2", c: "3" } Simple logic: The array has key–value pairs, so we loop through it two elements at a time. #JavaScript #FrontendDeveloper #CodingInterview
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development