Tags

  • AWS (7)
  • Apigee (3)
  • ArchLinux (5)
  • Array (6)
  • Backtracking (6)
  • BinarySearch (6)
  • C++ (19)
  • CI&CD (3)
  • Calculus (2)
  • DesignPattern (43)
  • DisasterRecovery (1)
  • Docker (8)
  • DynamicProgramming (20)
  • FileSystem (11)
  • Frontend (2)
  • FunctionalProgramming (1)
  • GCP (1)
  • Gentoo (6)
  • Git (15)
  • Golang (1)
  • Graph (10)
  • GraphQL (1)
  • Hardware (1)
  • Hash (1)
  • Kafka (1)
  • LinkedList (13)
  • Linux (27)
  • Lodash (2)
  • MacOS (3)
  • Makefile (1)
  • Map (5)
  • MathHistory (1)
  • MySQL (21)
  • Neovim (10)
  • Network (66)
  • Nginx (6)
  • Node.js (33)
  • OpenGL (6)
  • PriorityQueue (1)
  • ProgrammingLanguage (9)
  • Python (10)
  • RealAnalysis (20)
  • Recursion (3)
  • Redis (1)
  • RegularExpression (1)
  • Ruby (19)
  • SQLite (1)
  • Sentry (3)
  • Set (4)
  • Shell (3)
  • SoftwareEngineering (12)
  • Sorting (2)
  • Stack (4)
  • String (2)
  • SystemDesign (13)
  • Terraform (2)
  • Tree (24)
  • Trie (2)
  • TwoPointers (16)
  • TypeScript (3)
  • Ubuntu (4)
  • Home

    A Review of ES6 - New Built-In Methods, Promises

    Published Oct 13, 2019 [  Node.js  ]

    New Built-In Methods

    Object Property Assignment

    New function for assigning enumerable properties of one or more source objects onto a destination object.

    var dest = { quux: 0 }
    var src1 = { foo: 1, bar: 2 }
    var src2 = { foo: 3, baz: 4 }
    Object.assign(dest, src1, src2)
    dest.quux === 0
    dest.foo  === 3
    dest.bar  === 2
    dest.baz  === 4
    

    Array Element Finding

    New function for finding an element in an array.

    [ 1, 3, 4, 2 ].find(x => x > 3) // 4
    [ 1, 3, 4, 2 ].findIndex(x => x > 3) // 2
    

    String Repeating

    New string repeating functionality.

    " ".repeat(4 * depth)
    "foo".repeat(3)
    

    String Searching

    New specific string functions to search for a sub-string

    "hello".startsWith("ello", 1) // true
    "hello".endsWith("hell", 4)   // true
    "hello".includes("ell")       // true
    "hello".includes("ell", 1)    // true
    "hello".includes("ell", 2)    // false
    

    Number Type Checking

    New functions for checking for non-numbers and finite numbers

    Number.isNaN(42) === false
    Number.isNaN(NaN) === true
    
    Number.isFinite(Infinity) === false
    Number.isFinite(-Infinity) === false
    Number.isFinite(NaN) === false
    Number.isFinite(123) === true
    

    Number Safety Checking

    Checking whether an integer number is in the safe range, i.e., it is correctly represented by JavaScript (where all numbers, including integer numbers, are technically floating point number).

    Number.isSafeInteger(42) === true
    Number.isSafeInteger(9007199254740992) === false
    

    Number Comparison

    Availability of a standard Epsilon value for more precise comparison of floating point numbers.

    console.log(0.1 + 0.2 === 0.3) // false
    console.log(Math.abs((0.1 + 0.2) - 0.3) < Number.EPSILON) // true
    

    Number Truncation

    Truncate a floating point number to its integral part, completely dropping the fractional part.

    console.log(Math.trunc(42.7)) // 42
    console.log(Math.trunc( 0.1)) // 0
    console.log(Math.trunc(-0.1)) // -0
    

    Number Sign Determination

    Determine the sign of a number, including special cases of signed zero and non-number.

    console.log(Math.sign(7))   // 1
    console.log(Math.sign(0))   // 0
    console.log(Math.sign(-0))  // -0
    console.log(Math.sign(-7))  // -1
    console.log(Math.sign(NaN)) // NaN
    

    Promises

    Promise Usage

    First class representation of value that may be made asynchronously and be available in the future.

    function msgAfterTimeout (msg, who, timeout) {
        return new Promise((resolve, reject) => {
            setTimeout(() => resolve(`${msg} Hello ${who}!`), timeout)
        })
    }
    msgAfterTimeout("", "Foo", 100).then((msg) =>
        msgAfterTimeout(msg, "Bar", 200)
    ).then((msg) => {
        console.log(`done after 300ms:${msg}`)
    })
    

    Promise Combination

    Combine one or more promises into new promises without having to take care of ordering of the underlying asynchronous operations yourself.

    function fetchAsync (url, timeout, onData, onError) {
        
    }
    let fetchPromised = (url, timeout) => {
        return new Promise((resolve, reject) => {
            fetchAsync(url, timeout, resolve, reject)
        })
    }
    Promise.all([
        fetchPromised("https://backend/foo.txt", 500),
        fetchPromised("https://backend/bar.txt", 500),
        fetchPromised("https://backend/baz.txt", 500)
    ]).then((data) => {
        let [ foo, bar, baz ] = data
        console.log(`success: foo=${foo} bar=${bar} baz=${baz}`)
    }, (err) => {
        console.log(`error: ${err}`)
    })
    

    References: