Sushrut Mishra
Sushrut Mishra

@SushrutKM

10 Tweets 1 reads Nov 09, 2022
Important JavaScript Concept for Interviews - 'Promise'
๐Ÿ‘‰ Promise in JS
๐Ÿ‘‰ Asynchronous Operations
๐Ÿ‘‰ States of Promise
๐Ÿ‘‰ Promise Consumers
A Thread ๐Ÿงต๐Ÿ‘‡
๐Ÿ“Œ Promise
A Promise is an object that represents the eventual completion or failure of an asynchronous operation, and its resulting value.
As JS is single-threaded, scripts run one after another. Hence, there is an abundance of asynchronous jobs.
๐Ÿ“Œ Asynchronous Operations
The single-threaded functionality of JS stops you from performing long operations.
Using asynchronous JavaScript (such as callbacks, promises, and async/await), you can perform long network requests without blocking the main thread.
๐Ÿ“Œ Promises prove to be the ideal choice for handling asynchronous operations in JavaScript.
They can handle multiple asynchronous operations easily and provide better error handling
Benefits - Code Readability, handling of asynchronous operations, Error Handling
๐Ÿ“Œ States of Promise
1. Fulfilled: Action related to the promise succeeded.
2. Rejected: Action related to the promise failed.
3. Pending: Promise is still pending.
๐Ÿ“Œ Promise Syntax
๐Ÿ“Œ Parameters
Promise takes the Callback function as its parameter. Similarly, callback takes two parameters -
๐Ÿ‘‰ Resolve
๐Ÿ‘‰ Reject
When the operation goes as expected -> Resolve
If there is something wrong -> Reject
๐Ÿ“Œ Promise Example Code
In case of Resolve
๐Ÿ“Œ Promise Example Code
In case of Reject
That's a wrap!
If you enjoyed this thread:
1. Follow me @SushrutKM for more of these
2. RT the tweet below to share this thread with your audience

Loading suggestions...