Skip to content

Instantly share code, notes, and snippets.

@erikpantzar
Created October 28, 2019 17:33
Show Gist options
  • Save erikpantzar/1f2d818292470c210af9a37aa5d558ab to your computer and use it in GitHub Desktop.
Save erikpantzar/1f2d818292470c210af9a37aa5d558ab to your computer and use it in GitHub Desktop.

whatis?

variables

  var secret = "farted"
  let location = 'Living room'
  const literal = `I just ${farted} in the ${location}`

datatypes and how to manipulate

  • strings
  • object
  • array
// example of STRINGS

const name = 'Erik'
const surname "Erikssson"
const fullName = `${name} ${surname}`

// Example of OBJECT and how to interact
const person = {
  name: "Erik",
  says: function(message) {
    console.log('Erik says: ' + message)
  }
}

// array
const brothers = ['erik', 'anton']
const first = brothers[0] // erik
const seconds = brothers[1] // anton
const howMany = brothers.length // 2

functions

  function add(a, b) {
    return a + b
  }

  const multiply = (a,b) => a * b

  function whatIsMyMonthlySalary(hourRate, hoursWorked) {
    return multiply(hourRate, hoursWorked)
  }

  whatIsMyMonthlySalary(200, 160)

Working with data

API

An application program interface (API) is a set of routines, protocols, and tools for building software applications. Basically, an API specifies how software components should interact. Additionally, APIs are used when programming graphical user interface (GUI) components.

API Endpoint

http://api.weather.com/getWeatherByName?c=Stockholm

How to build the API you build functions that handle information and return data depending in input and or HTTP method ('GET', 'POST', 'DELETE').

HTTP

Using fetch to get data

Promises

A promise is an object that may produce a single value some time in the future: either a resolved value, or a reason that it’s not resolved (e.g., a network error occurred).

Working with primises

  const promise = fetch('url')

  promise.then(response => {
    // deal with response
  })

Async await

https://javascript.info/async-await

There’s a special syntax to work with promises in a more comfortable fashion, called “async/await”. It’s surprisingly easy to understand and use.

  async function getData() {
    const weather = await fetch(url)
      .then(res => res.json())
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment