This community-built FAQ covers the “The async Keyword” exercise from the lesson “Async Await”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Web Development
Asynchronous JavaScript
FAQs on the exercise The async Keyword
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Need broader help or resources? Head here.
Looking for motivation to keep learning? Join our wider discussions.
Learn more about how to use this guide.
Found a bug? Report it!
Have a question about your account or billing? Reach out to our customer support team!
None of the above? Find out where to ask other questions here!
async function withAsync(num){
if (num === 0){
return 'zero';
} else {
return 'not zero';
}
}
withAsync(100)
.then((resolveValue) => {
console.log(` withAsync(100) returned a promise which resolved to: ${resolveValue}.`);
})
after calling withAsync(100) why don’t we include also .catch() as a rejected outcome. Or why do we only use .then()?
7 Likes
Why use Promise over async…await?
is it just for compatibility at this point? Or are there benefits to using Promise
5 Likes
It’s just so annoying when you get the exercise right, but codecademy won’t take your answer for some reason. I press get solution to this, code is identical fml.
6 Likes
I don’t think there’s any real advantage, just syntactic sugar - easier to read and understand.
I think that lower part would be better written with await inside withAsync’s body. It’s probably just a placeholder before await is introduced.
What is meant by:
If there’s a non-promise value returned from the function, it will return a promise resolved to that value.
What’s the difference between a value and a promise resolved to a value?
For once, I think the course is well made.
I followed another one on this topic and it was really confusing…
async function withAsync(number) {
return number ? "not zero" : "zero";
}
2 Likes
Is it not possible to combine async with an arrow function?
I tried:
async const withAsync = num => {
if (num === 0) {
return 'zero';
} else {
return 'not zero';
}
}
But unexpected token on const
and then I tried:
async withAsync = num => {
if (num === 0) {
return 'zero';
} else {
return 'not zero';
}
}
Unexpected identifier on withAsync
, so I guess not…
It seems like we need to declare async on the function, rather than the variable, so if we wanted to use it with arrow functions then we’d put:
const withAsync = async num => {
}
See:
4 Likes
Is it normal in the example we have two “resolve” as argument and not reject if it’s not equal to zero?
function withConstructor(num){
return new Promise((resolve, reject) => {
if (num === 0){
resolve('zero');
} else {
resolve('not zero');
}
})
}
withConstructor(0)
.then((resolveValue) => {
console.log(` withConstructor(0) returned a promise which resolved to: ${resolveValue}.`);
})
2 Likes
The difference is not in the outcome but in the process the computer took to returns that value.
For instance the following will return the same value but the process is different:
onsole.log(3)
console.log(1+2)
I prefer using the ternary operator! i think its easier
1 Like
We don’t have 2 resolved values, but 1 which depends on a condition. The reason we aren’t rejecting a value is because we aren’t handling errors at the moment. We are simply saying “If the num argument is equal to 0, return this resolved value, and if it’s not equal to 0, return this resolved value”
If you wanted to take it a step further, you could use the reject function to return an error if the num argument isn’t a number. To achieve this, you could do something like this:
function withConstructor(num){
return new Promise((resolve, reject) => {
if (typeof num !== "number") {
reject("Error: passed in argument isn't of type 'number'")
} else if (num === 0){
resolve('zero');
} else {
resolve('not zero');
}
})
}
2 Likes
Yes, happens more often than one would think. Copy your code, reset the exercise, and paste. It’s second nature to me now.
OK this has happened on EVERY SINGLE EXERCISE from
Whenever i try to run node I get this error
and everytime I spend a while scratching my head, going through every line of code looking for the difference to the solution and finding there is none, but the only thing that fixes it is replacing my code with solution button.
AFAICS my code is identical, but I will have to replace it to get it to work.
Edit. Saw someone else’s solution to copy work, refresh page and paste back in. It works, but something is definitely going wrong with your node js.
4 Likes
As said the intro, Promise
is ES6 while async/await
is ES8. It’s a pure syntactic choice. Your Babel transpiler will convert it into ES5 in both cases.
I’m getting this regularly too. I think my internet connection drops periodically, which is causing this. I would say it’s a bug on Codecademy’s side.
1 Like
I really liked this lesson. Nicely explained and boy does async
make things easier
Hello uzikim1, how are you?
I’m Brazilian, and my English isn’t perfect, but if you can understand me, that’s cool!
Unlike a promise that returns a resolved or rejected value depending on conditions, async only returns ‘resolved’ values. If you add a catch, it will not return any value for the argument!
2 Likes