Array.map() is a glorified for() loop. It iterates through an array completely, applying the provided function to each element in series. It returns a new array, and does not modify the original. It is extremely useful for things like parsing api objects, adding formatting to strings, or performing math on a list. source
Array.reduce() iterates through each item in an array, and returns a single "accumulator" value. Each iteration, it applies the provided function given an accumulator (like a running total), current element, and index. You may provide an initial value for the accumulator, otherwise the default action is to use the first array value. This method is super useful for things like summing a list of numbers, or changing the shape of data. source
const superagent = require('superagent')
superagent.get('https://swapi.dev/api/people')
.then(result => console.log(result.body))
.catch(console.error);
const superagent = require('superagent')
Async function getCharacters() {
const character = await superagent.get('https://swapi.dev/api/people');
console.log(character);
}
Promises are a way for JavaScript to handle processes that take too long to happen in-line. By choosing to have a function return a promise, we can tell javascript "do this work, and when you finish just let me know and give me the data." This lets you choose how to handle the data once it's returned. This is basically the same as what a callback function does, but writes much more semantically and is therefore easier to manage compared to extensive callback use (callback hell).
All promises must be either Resolved or Rejected, which we handle with .then()
and .catch()
, respectively. This allows us to take different actions depending on the result of the function.
source.
Not necessarily! Plenty of callback functions are able to be run synchronously - asynchrony depends upon if the function returns a promise, or if it can simply be run inline.