reading-notes

this repo will contain my reading during the course .

View on GitHub

Node Ecosystem, TDD, CI/CD

What Array.map() does:

The map() method creates a new array with the results of calling a function for every array element.

The map() method calls the provided function once for each element in an array, in order.

map() does not execute the function for empty elements.

map() does not change the original array.

map() is fully supported in all modern browsers

What Array.reduce() does:

The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value. img1

The reducer function takes four arguments:

  1. Accumulator
  2. Current Value
  3. Current Index
  4. Source Array Your reducer function’s returned value is assigned to the accumulator, whose value is remembered across each iteration throughout the array, and ultimately becomes the final, single resulting value.

SuperAgent

SuperAgent is light-weight progressive ajax API crafted for flexibility, readability, and a low learning curve after being frustrated with many of the existing request APIs. It also works with Node.js!

request(‘GET’, ‘/search’).then(success, failure);

// pattern: https?+unix://SOCKET_PATH/REQUEST_PATH // Use %2F as / in SOCKET_PATH try { const res = await request .get(‘http+unix://%2Fabsolute%2Fpath%2Fto%2Funix.sock/search’); // res.body, res.headers, res.status } catch(err) { // err.message, err.response }

ES2017+: Promises with async/await

The ECMAScript version released in 2017 introduced syntax-level support for asynchronous functions. With the help of async and await, you can write asynchronous in a “synchronous style”. The code is still asynchronous, but it’s easier to read/understand.

async/await builds on top of promises: an async function always returns a promise. await “unwraps” a promise and either result in the value the promise was resolved with or throws an error if the promise was rejected.

Here is an example that elaborates the delay function findItem() above:

// Using ‘superagent’ which will return a promise. var superagent = require(‘superagent’)

// This is isn’t declared as async because it already returns a promise function delay() { // delay returns a promise return new Promise(function(resolve, reject) { // Only delay is able to resolve or reject the promise setTimeout(function() { resolve(42); // After 3 seconds, resolve the promise with value 42 }, 3000); }); }

async function getAllBooks() { try { // GET a list of book IDs of the current user var bookIDs = await superagent.get(‘/user/books’); // wait for 3 seconds (just for the sake of this example) await delay(); // GET information about each book return await superagent.get(‘/books/ids=’+JSON.stringify(bookIDs)); } catch(error) { // If any of the awaited promises was rejected, this catch block // would catch the rejection reason return null; } }

// Start an IIFE to use await at the top level (async function(){ let books = await getAllBooks(); console.log(books); })();

A promise is basically an advancement of callbacks in Node. While developing an application you may encounter that you are using a lot of nested callback functions.

Are all callback functions considered to be Asynchronous?

Well, basically yes, but there’s a catch. Every asynchronous function takes a function argument, but not every function that does so is asynchronous. It matters how the argument is used inside the function. For a function to be asynchronous it needs to perform an asynchronous operation. It needs to incorporate the argument callback in handling the results of this asynchronous operation. Only this way the function becomes asynchronous.

For example a function reading contents of a file with the correct encoding can be implemented as an asynchronous function.