Credits :codeSTACKr
it’s essential to learn how promises work before learning async await since async await is basically just syntactic sugar wrapped around promises making them easier to work with now that we have learned about promises and fetch I’ll show you how we can rewrite this fetch statement using async await so it without async await we use then to wait on the data to be returned this is fine for simple request but when we are dealing with multiple promises it can get a bit confusing as to when each line will get executed with async await we can write a code that looks synchronous but it’s actually a synchronous the first thing that we must do is wrap or a synchronous code in a function and we add the async keyword before the function now within the function we can create a response constant and use the awake keyword before our promise which in this case is our fetch statement now in the next line we do the same thing awaiting our wrist

wants. Jason on the last line we are logging the Jason data this line will wait on the previous lines to complete and then after the function we call it so this is very logical in the way that it is written and easy-to-understand note that you cannot use the await keyword without the async keyword attached to the function these only work together the function itself can be a standard function like this or it can be a narrow function like this or even an anonymous function that calls itself like this so understanding how promises and async await work together can make your coat cleaner and more logical this is been a 90-second JavaScript January

Credits :Fireship
async await Feels Like Heaven because it avoids the Callback hell or Pyramid of Doom by writing asynchronous code in a clean line by line format that is until are handling comes into play because you end up with the try catch Tower of Terror all your beautiful one-liners magically expand to at least five lines of code if you find yourself here that easy way out is to attend the catch method it to the end of each promises better but I can still get repetitive another option is to create a function that implements one try catch to replace all the other separate results the promised then returns in a ray where the first element is the date at and the second element is an error but if there’s an error than the data is known for any errors to find now when you call this function in your code you can be structure it to get a clean one liner with are handling for use a regular that statement if you want to do something else with the air
Add a review

Origin Ten  Ltd,
Queens House
200 Lower High Street
Watford
WD17 2EH

Stay tuned

Exceptional quality. Ethical factories. Radical Transparency. Shipping and returns.

    Privacy Preference Center