Group Chat Week 5 Solutions
Well done if you got these right!
-
How would we rewrite this code with the ternary operator?
- My solution here.
-
How would we rewrite this code with the Array.forEach prototype method?
- My solution here.
-
How would we rewrite this code using a class?
- My solution here.
-
What will running this code output and why?
- This will resolve to the request object that we get back from making a GET request to our URL. To get the actual data, we need to call the
.json()
method on this object, which in turn will return a Promise.
- This will resolve to the request object that we get back from making a GET request to our URL. To get the actual data, we need to call the
-
What will running this code output and why?
- This returns a (still pending!) Promise. The reason for this isn’t super obvious. We’re console.log-ing the data variable as soon as we have access to it. At that moment in time, the value is still the value of the Promise from line 7, which is still set to “pending”.
- If we actually want to use a promise like this, then we’ll need to use it inside of this function, rather than trying to pass it on as a result - since this will always return a Promise.