๐ค What are Promises?
๐๏ธ Well, promises are Javascript objects that may have some value in the future.
๐ Didn't understand? Don't worry, let's take an example:
๐ฅฒ Suppose you're a student and you're bad at Chemistry (I didn't have to 'suppose' back in my school days).
๐๏ธ Well, promises are Javascript objects that may have some value in the future.
๐ Didn't understand? Don't worry, let's take an example:
๐ฅฒ Suppose you're a student and you're bad at Chemistry (I didn't have to 'suppose' back in my school days).
๐ช
Your chemistry teacher is concerned and she wants you to score good marks.
๐ So she makes a 'promise' to you saying if you score more than 90 marks in the upcoming test, she'll give you a prize.
๐ Now, she hasn't given you any prize yet, but she'll if you score good marks!
๐ So she makes a 'promise' to you saying if you score more than 90 marks in the upcoming test, she'll give you a prize.
๐ Now, she hasn't given you any prize yet, but she'll if you score good marks!
๐ญ Promises in Javascript are quite similar.
โณ Many times, we need to give some time for an operation to finish (think of letting an API fetch some data in the background).
๐ We can't make the user wait for that.
โณ Many times, we need to give some time for an operation to finish (think of letting an API fetch some data in the background).
๐ We can't make the user wait for that.
๐ So what we do is that we make use of a promise and Javascript continues executing knowing that it'll get a result (like how the teacher will give a prize, in the future)
๐ก This way, we get to do any operation that is going to take time without halting everything else.
๐ก This way, we get to do any operation that is going to take time without halting everything else.
๐ Now, promises have three states:
๐ก Pending
โ Resolve
โ Reject
๐ค They're all quite simple but let's understand each one of them:
๐ก Pending
โ Resolve
โ Reject
๐ค They're all quite simple but let's understand each one of them:
โฑ๏ธ Pending:
๐ค It is exactly what it sounds like and it is the default state of a promise until a promise is completed either successfully or unsuccessfully.
๐ So, unless the job is performed successfully or unsuccessfully, the default state of a promise is 'pending'.
๐ค It is exactly what it sounds like and it is the default state of a promise until a promise is completed either successfully or unsuccessfully.
๐ So, unless the job is performed successfully or unsuccessfully, the default state of a promise is 'pending'.
โ
Resolve:
๐ค Read resolve, think success, because when the job is successfully completed, the state of a promise changes from 'pending' to 'resolved', since the promise has been 'resolved'.
๐ So, from now on, read resolve, think success!
๐ค Read resolve, think success, because when the job is successfully completed, the state of a promise changes from 'pending' to 'resolved', since the promise has been 'resolved'.
๐ So, from now on, read resolve, think success!
โ Reject:
๐ It does exactly what it sounds like.
โ When the pending task in a promise fails, the state of the promise is 'reject' as the task was 'rejected'.
โจ That's how simple it is!
๐ It does exactly what it sounds like.
โ When the pending task in a promise fails, the state of the promise is 'reject' as the task was 'rejected'.
โจ That's how simple it is!
๐ Also, note that a promise returns an object by default and you're good to go.
๐ฏ This is a part 1/2 of my promise thread and I 'promise' to do the second part tomorrow.
๐ The current state of my promise is 'pending' but it'll be 'resolved' tomorrow.
๐ค Stay tuned!
๐ฏ This is a part 1/2 of my promise thread and I 'promise' to do the second part tomorrow.
๐ The current state of my promise is 'pending' but it'll be 'resolved' tomorrow.
๐ค Stay tuned!
๐ I write daily threads on web dev so if you enjoyed this thread, please:
โ Follow me @sumitsaurabh927 for more of these.
โ RT the tweet below to show me some ๐งก and comment your thoughts.
๐ Your each like, every comment motivates me to come up with awesome new stuff!
โ Follow me @sumitsaurabh927 for more of these.
โ RT the tweet below to show me some ๐งก and comment your thoughts.
๐ Your each like, every comment motivates me to come up with awesome new stuff!
Loading suggestions...