help with asynchronous javascript
why when i run this and check the console, and click the promise object before 1.5second, it stays pending forever, but if i click the promise object after 1.5 seconds, it fulfilled
26 Replies
That's because you're logging the value of
promise
at that moment in time. console.log
isn't real-time. But when you click on it it forces an internal refresh of the Promise
object and shows the fulfilled state.
What you should be doing instead is this:
That way, after the second and a half, it'll log done
to the console (the returned value of your resolver).
Note that promise.then(console.log);
is the point-free version of promise.then( (value) => console.log(value));
. If you're calling a function within a function and passing the value to the inner function you can remove the outer and inner ()
and it'll call the function directly.ohhhh
Console logs of objects can be confusing because you can actually get two values from a single log. The value at the time that the log was run is the value you get if you don't click on the triangle. The value that you get when you click the triangle is the current value at the time when you click on it.
ohhh
when javascript.info says:
"Methods Promise.resolve and Promise.reject are rarely needed in modern code, because async/await syntax (we’ll cover it a bit later) makes them somewhat obsolete.
We cover them here for completeness and for those who can’t use async/await for some reason."
do they mean
is also outdated?
cuz they used this in the majority of like 4 chapters of the promises section and now theyre saying its outdated😭
It’s not outdated, it’s more that most JS devs don’t understand promises so the async/await syntax was designed to make it look more like what they expect code to look like.
Both can and are used in production all over the place. It’s more of a stylistic choice than one being outdated.
the use of async/await also reduce the amount of callback hell
if you need to do 2+ async operations in a row, then the async/await can be vastly superior
but you need to be in an async function to use await
That’s partially a skill issue. You can reduce callback hell by using declared functions instead of inlining everything.
that's true, but the equivalent of this:
is something like this:
not too bad, right?
but what about
return async fetch(...)
?😒
but in javascript.info it was said that to avoid callback hell we use promises
a promise receives a callback
Using callbacks is inevitable. Callback hell is when you have so many callbacks you can't tell left from right.
yup, very common with dogwater apis written before promises existed, like indexeddb
(which is why people use wrappers on top of it, so you can use promises or async/await with it)
Callback hell is when you have so many callbacks you're indented like 8 or 9 times (or more 🙀 ) and can't remember what context you're in. Promises help to flatten that by passing data from one
.then()
method to another. You still use a callback function in each .then()
(even if an anonymous one), however, since that's how JS works 🤷the async/await is syntactic sugar for it all, and makes a promise in the background anyways, but the code may look neater
i would call it "hell" if you are more than 2 deep
Yeah, the 8+ was a bit of exaggeration. I concur that 3+ is hell
depending on the api, it might be possible to go that deep
Here's a great article from 20019 from Zell Liew about it, showcasing what callback hell is, how to avoid it with promises, as well as async/await:
https://www.freecodecamp.org/news/how-to-deal-with-nested-callbacks-and-avoid-callback-hell-1bc8dc4a2012/
freeCodeCamp.org
How to deal with nested callbacks and avoid “callback hell”
JavaScript is a strange language. Once in a while, you have to deal with a callback that’s in another callback that’s in yet another callback. People affectionately call this pattern the callback hell. It kinda looks like this: firstFunction(args, fu...
The example given is making a burger :p
that is actually a perfectly interesting way to put it
the console.log result is just
why i didnt receive the data
That’s because you need to
await response.json()
. It’s a function. That’s what the console is telling youohhhhhhhhh
and i was cursing the website i used for the api for like 3 hours😭
no, you just didn't execute the function
One thing you forgot to do was check for errors:
Or using promise chaining:
And, of course, the
.then()
callbacks can be named functions, too:
And now it almost reads like a sentence! "Fet from URL. Then check if it's ok. Then get JSON from the response. Then log it. Else log the error.yeah i did it in the updated code