20day Coding Challenge Day 1 APIs
Published: 2021-12-24 09:43:07

# Follow along my learning from CSS to React and Node in 20 days

**Hello!**

My name is Sebastian, and I'm a high-school student from Sweden. I have dabbled with some front-end development for the past few months. And since Christmas brake finally arrived today, I've decided to start a challenge for myself! I'm going to code non-stop for the coming 20 days. 

I'm following the front-end career path at  [Scrimba](https://scrimba.com/learn/frontend) and I'm currently at module 7 (APIs)

By the end of the 20 days I'm hoping to have finished the front-end career path and have started with some backend Node.js development.
I will be documenting my progress for the coming days, and filming myself on my  [Youtube-channel](https://www.youtube.com/channel/UCikWIcChAOSwoc2qpbZ6iIA), so feel free to follow along my journey and learn with me!

## Day 1 APIs

Today I started and finished the  [Async Javascript and War](https://scrimba.com/playlist/pMr5psY)  Playlist on Scrimba. It began with explaining the basics of callbacks. Which is essentially a function calling another function.


```javascript
function getValueA(A) {
    return A + 1;
}

function getValueB(B) {
    return getValueA(B)
}
```



By doing this, we can easily control the flow of our code. This is thanks to the fact that we now can run functions when the other functions first have finished. Thus it's the first step towards asynchronous programming(running things in a reorganized fashion to maximize effectiveness).

This way of chaining together functions becomes very problematic and messy fast though :(

![Callback Hell](<https://miro.medium.com/max/721/1*zxx4iQAG4HilOIQqDKpxJw.jpeg>)

To fix this, promises were made. As an analogy, think of it this way. Once you have submitted your resume to a company, they *promise*, to return to you in one week. Whilst you are waiting for their response, the promise is in a pending state. If they respond to you within one week, then the promise was *successful*, but if they never contacted you again, then the promise was *rejected*. 

Here's a code example:

```javascript
fetch("https://bestapi.com")
    .then(response => response.json) //Turning data from JSON to JavaScript
    .then(data => console.log(data))
    .catch(error => console.log(error))
``` 

Here, the code makes a request to an API using fetch, if that API successfully responded, represented by the .then(), it calls an anonymous function that turns the data from JSON to JavaScript. If that was successfully completed, it then logs that data to the console, if it was unsuccessful, represented by the .catch() method, it logs the error message instead.

Thus, it's now much simpler to write asynchronous code: 
![.then() chaining](<https://cdn-images-1.medium.com/max/928/1*1rlNjMeVyQJhA0EdgJL1sw.png>)

Using this, I built the War -Card Game from the  [Scrimba Tutorial](https://scrimba.com/learn/frontend/war-display-the-final-winner-co12346239b40dc7fc1a0b3d6) : 

![Game of War - card game](https://cdn.hashnode.com/res/hashnode/image/upload/v1640205518994/jVSfzB-ts.png)

Finally, to add some syntactic sugar on top of this, we can use async/await to make things a bit nicer.
(An example using a fetch request to generate a random nature image from unsplash.com)

Normal Promise:
```javascript
function generateImage() {
    fetch("https://apis.scrimba.com/unsplash/photos/random?orientation=landscape&query=nature")
        .then(res => res.json())
        .then(data => {
            document.body.style.backgroundImage = `url(${data.urls.full})`
        })
}
``` 
vs Async/Await:
```javascript
async function generateImage() {
    const response = await fetch("https://apis.scrimba.com/unsplash/photos/random?orientation=landscape&query=nature");
    const data = await response.json();
    document.body.style.backgroundImage = `url(${data.urls.full})`
}
``` 

I finished my day off by building a landing page from Brad Traversy's 50 Projects in 50 days [ udemy course](https://www.udemy.com/course/50-projects-50-days)


![Playstation vs Xbox Landing Page](https://cdn.hashnode.com/res/hashnode/image/upload/v1640206445514/JPvHIfodg.png)

That was it for today! I found myself procrastinating a bit throughout the day, but I'm going to sharpen up for tomorrow ;)

Peace.