Bifalgorithm post

So let's take const sub equals new promise. So we have this promise keyword in JavaScript promise class in JavaScript and we're going to create a new instance for this promise. We're going to add this new keyword over here new promise and this promise takes a callback which has a resolve and reject keywords inside of it. Now inside of this, we can write our asynchronous code like we're trying to fetch an API or whatever that we're trying to do that will return some promise which will be fulfilled or rejected. So we can write that code over here. So for now, let's just write our set timeout code because that's an easy example of an asynchronous code. Right. So let's give it 2000 milliseconds and inside of it, I'm going to take a result variable, which is true for now. And if the result is true, then then we're gonna resolve this promise that means the promise is full fill and to resolve this we're gonna use this resolve keyword over here so I'm gonna say resolve and subscribe to roadside coder but what if the result is false what if the guy is not subscribed to the roadside coder then we're gonna simply reject the promise then we're gonna say else reject and we're gonna throw a new error new error why aren't you subscribed to roadside coder Then we're going to simply reject the promise. Then we're going to say, else reject and we're going to throw a new error, new error. Why aren't you subscribed to roadside code? Yeah, that's a good question. So yeah, let's try to run this and how do we run this? How do we execute a promise? And I'll show you one good thing that I like about promise. So if we execute the promise, we can say dot then, then means if the promise is fulfilled, we will get the result in then. And if the promise is not fulfilled will get the result in then and if the promise is not fulfilled if it fails then we're gonna get the result in catch so you see how this promise handles error right out of the box so and then I'm gonna say rest log rest answer this error so I'm gonna say console dot error err so let's see let out. And since as you already know, this is an asynchronous operation, then this will first run the synchronous code and then the asynchronous code over here. Start, stop, and then subscribe to roadside coder. Great. But what if our result was false, then we will get an error over here. Why aren't you subscribed to Roadside Coder.

So let's take const sub equals new promise. So we have this promise keyword in JavaScript promise class in JavaScript and we're going to create a new instance…

Ideas in this flowchart

  • JavaScript Promise Example
  • Creating a Promise
  • Promise Keyword
  • New Instance with Callback
  • Asynchronous Code
  • Set Timeout Example
  • Result Variable
  • Result is True
  • Resolve Promise
  • Result is False
  • Reject Promise with Error
  • Executing a Promise
  • Then Method
  • Catch Method
  • Error Handling
  • Synchronous vs Asynchronous
Feed/

So let's take const sub equals new promise. So we have this promise keyword in JavaScript promise class in JavaScript and we're going to create a new instance for this promise. We're going to add this new keyword over here new promise and this promise takes a callback which has a resolve and reject keywords inside of it. Now inside of this, we can write our asynchronous code like we're trying to fetch an API or whatever that we're trying to do that will return some promise which will be fulfilled or rejected. So we can write that code over here. So for now, let's just write our set timeout code because that's an easy example of an asynchronous code. Right. So let's give it 2000 milliseconds and inside of it, I'm going to take a result variable, which is true for now. And if the result is true, then then we're gonna resolve this promise that means the promise is full fill and to resolve this we're gonna use this resolve keyword over here so I'm gonna say resolve and subscribe to roadside coder but what if the result is false what if the guy is not subscribed to the roadside coder then we're gonna simply reject the promise then we're gonna say else reject and we're gonna throw a new error new error why aren't you subscribed to roadside coder Then we're going to simply reject the promise. Then we're going to say, else reject and we're going to throw a new error, new error. Why aren't you subscribed to roadside code? Yeah, that's a good question. So yeah, let's try to run this and how do we run this? How do we execute a promise? And I'll show you one good thing that I like about promise. So if we execute the promise, we can say dot then, then means if the promise is fulfilled, we will get the result in then. And if the promise is not fulfilled will get the result in then and if the promise is not fulfilled if it fails then we're gonna get the result in catch so you see how this promise handles error right out of the box so and then I'm gonna say rest log rest answer this error so I'm gonna say console dot error err so let's see let out. And since as you already know, this is an asynchronous operation, then this will first run the synchronous code and then the asynchronous code over here. Start, stop, and then subscribe to roadside coder. Great. But what if our result was false, then we will get an error over here. Why aren't you subscribed to Roadside Coder.

"So let's take const sub equals new promise."

👍
0
0 comments0 shares

Sign in to view and post comments.