Skip to content

Latest commit

 

History

History
46 lines (35 loc) · 2.27 KB

File metadata and controls

46 lines (35 loc) · 2.27 KB

Node Ecosystem, TDD, CI/CD

Array Map

Using the map method on an array will loop through the array that the method is called on and for each index of that array perform some action provided via an arrow function, returning the output in a new array of the same index. So the output will be a new array.

Array Reduce

Using the reduce method on an array will loop through the array that the method is called on and for each index of that array utilize a reducer and a current value of the index. Some modification can be made to the reducer value, which is then returned, and modified at the next index in the array. This continues until it has looped through the whole array and returns the single reducer value.

Superagent Snippets

then():

function getCharacters() {
const characterURL = {};
superagent.get(`https://swapi.dev/api/people`)
  .then( data => {
    // console.log(data.body.results);
    data.body.results.forEach(charObj => {
      characterURL[charObj.name] = charObj.url;
    });
  console.log(characterURL);
  return characterURL;
  })
  .catch(err => console.error(err));
}

async/await:

  const cityDataGetter = async (cityName) => {
  let cityData = await superagent.get(`https://geocode.xyz/${cityName}?json=1`);
  // console.log(cityData.body);
  console.log(`${cityData.body.standard.city} is located at a longitude of ${cityData.body.longt} and a latitude of ${cityData.body.latt}`);
  };
**```**

### Promises
[JS Promises for Beginners](https://www.freecodecamp.org/news/what-is-promise-in-javascript-for-beginners/)

A promise is a placeholder for an asynchronous task which is yet to be completed. When you define a promise object in JavaScript, instead of returning a value immediately, it returns a promise. While waiting the for the result of the Promise, the status is Pending. At the end of the task, the Promise will let you know if it was succesful or not, and based on the result you can have certain callback functions executed.


### Callback Functions
I think callback functions are both sychronus or asychronus depending on how they are used. If a function is waiting on something, setTimeout for example, before they are executed, it is asynchronus, but if you call a function within another function that can immediately execute then its sychronus.