Previously on Coding Adventures - abukhalil-LTUC-ASAC/amman-401d4 GitHub Wiki

Adding to what have been discussed during 301 the journey of the backend technology has been rather exciting, contributing to expanded possibilities on what could be done using a server to do your logic work, and the front that displays the output. Test-drive development and continuous integration is something to look forward to learn, they seem like a pillar to modern practices.

A callback to some JS work during 301 have taught the following:

Array.map() moves through each element and replaces its content as you code it. Array.reduce() also moves through each element but returns a single value of your choosing in code.

Both of them are similar to a for loop, but one mutates its original input and changes it with input[i] = newVal, the other has a one single value return value.

superagent() is an ajax alternative to work in node.js, ajax is what you might have used in JavaScript files along with php. What they do is basically do callback on URL to fetch data from said URL in form on JSON objects. What you do later is up to you.

 superagent
   .post('/api/pet')
   .send({ name: 'Manny', species: 'cat' })
   .set('X-API-Key', 'foobar')
   .set('Accept', 'application/json')
   .then(res => {
      alert('yay got ' + JSON.stringify(res.body));
   });

.set values depends on the type of header variables that the API requests, all of these could be found in documentation to handle each API properly. Since these functions are asynchronous, as in they operate independent of the call stack through Web API, a proper implementation of async and await is needed so that the output could be displayed as intended.

async function getPets() {
 await superagent
   .post('/api/pet')
   .send({ name: 'Manny', species: 'cat' })
   .set('X-API-Key', 'foobar')
   .set('Accept', 'application/json')
   .then(res => {
      alert('yay got ' + JSON.stringify(res.body));
   });
}

Callbacks are function calls in other functions arguments, and are usually asynchronous but not always, in fact most callbacks are synchronous unless stated otherwise through the following code declaration:

  • timer functions setTimeout, setInterval
  • special functions nextTick, setImmediate
  • listening to network, querying a database
  • reading or writing, generally I/O from a resource
  • subscribing to an event

For more extensive reading, follow this link asynchronously when you'd wish to learn more.