Skip to content

Units-IHWDFT/m1_d08-js_async_callbacks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

logo_ironhack_blue 7

Async & callbacks

Module 1 - Day 8

Functions

At the nomenclature level, there are:

  • Nominal functions: they have declaration and invocation, with high reusability.
    function sayHi(name){
      console.log(`Hello, ${name}!`)
    }
  • Anonymous functions: no declaration, only invocation. Used in a single context.
    names.forEach(function(elm) => {
       console.log(`Hello, ${elm}!`)
    })

At the structure level, there are:

  • Function statement: they have the reserved word function, parentheses for their parameters and block:
    function getTotals(subtotal, tax){
      return subtotal + tax
    }
  • Arrow function: they lack the reserved word function, the parenthesis in their parameters can be omitted in front of a single parameter, and they have an arrow before the opening of their block (block can be omitted in functions of a single instruction).
    const getTotals = (subtotal, tax) => subtotal + tax

Functions can recieve arguments as parameters and return data. It is possible to implement as a last parameter the propagation operator to create an array with the remaining parameters:

const getInvoiceTotal = (tax, ...products) => tax + products.reduce((acc, product) => acc + product.price, 0)

Variable scope

  • Variables declared inside a block or function are called local or private, accessible only within that context.
  • Variables declared outside any block or function are called global or public, accessible from anywhere in the script.

Async programming

  • Javascript is a single-threaded language (single-threaded), so it cannot perform multiple tasks at the same time.
  • It is also synchronous, since tasks are executed one after the other following the program sequence, where high-cost tasks in terms of time generate natural bottlenecks in the flow.
  • Based on this nature, there are several asynchronous resources available to manage scheduled and/or parallel tasks. Among them, we find non-blocking methods such as .setTimeout() or .setInterval().

Callbacks

A callback is a function that receives as parameter another function, invoking it inside:

function foo = callback => callback()

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published