Lecture 7.2 - Introduction to Modern Javascript (4)-p1i71n6qb0edn1jii190mnd4
Lecture 7.2 - Introduction to Modern Javascript (4)-p1i71n6qb0edn1jii190mnd4
ECMA Script
Nisar Ahmed Siddiqui
ECMA Script
• JavaScript was invented by Brendan Eich in 1995, and became an
ECMA standard in 1997.
• ECMAScript is the official name of the language.
• ECMAScript versions have been abbreviated to ES1, ES2, ES3, ES5, and
ES6.
• Since 2016, versions are named by year (ECMAScript 2016, 2017,
2018, 2019, 2020).
New features in ES6
• With template literals, you can use both single and double quotes
inside a string:
Interpolation
• Template literals provide an easy way to interpolate variables and
expressions into strings.
• The method is called string interpolation.
• The syntax is:
OR
Default + Rest + Spread
• We can pass the default value to a parameter.
Default + Rest + Spread
• The rest operator is used to condense a group of elements down into
a single array.
• Let’s create a function that adds three numbers together using the
reduce array helper function.
Default + Rest + Spread
• The spread is closely related to rest parameters, because of … (three
dots) notation. It allows to split an array to single arguments which
are passed to the function as separate arguments.
Iterators & For…Of
• Already explained in detail in last classes
Generators
• Regular functions return only one, single value (or nothing).
• Generators can return (“yield”) multiple values, one after another, on-
demand. They work great with iterables, allowing to create data
streams with ease.
• To create a generator, we need a special syntax construct: function*,
so-called “generator function”.
Working of generators
Using a generator,
• We can stop the execution of a function from anywhere inside the
function and continue executing code from a halted position
Passing Arguments to Generator
Functions
Promises
"I Promise a Result!"
• "Producing code" is code that can take some time
• "Consuming code" is code that must wait for the result
• A Promise is a JavaScript object that links producing code and
consuming code
JavaScript Promise Object
• A JavaScript Promise object contains both the producing code and
calls to the consuming code:
• When the producing code obtains the result, it should call one of the two callbacks:
Promise Object Properties
• A JavaScript Promise object can be:
• Pending
• Fulfilled
• Rejected
Promise Object Properties (cont…)
• The Promise object supports two properties: state and result.
• While a Promise object is "pending" (working), the result is undefined.
• When a Promise object is "fulfilled", the result is a value.
• When a Promise object is "rejected", the result is an error object.
Promise How To
• Promise.then() takes two arguments, a callback for success and
another for failure.
• Both are optional, so you can add a callback for success or failure
only.
Example
JavaScript Promise Examples
(settimeout)
• To demonstrate the use of promises, we will use the callback
• Waiting for a Timeout
JavaScript Async
"async and await make promises easier to write“
Without reject
function
Async and await with timeouts