ES6: Return of Javascript

Promises

importThis(function(){ 
importThat(function(){
doSomethingElse(function(){
waitForEvent(function(){
respondWithSomething(function(){
save(function{
moveOn();
})
})
})
})
})
})
var promise = new Promise(function(resolve, reject) {}
var promise = new Promise(function(resolve, reject){ 
if(success()){
resolve("success");
}else {
reject("Failure");
}
});
promise.then(
function(result){
console.log(result); // "success"
},
function(err){
console.log(err); // Error: "Failure"
}
})
function get(url){ 
return new Promise(function(resolve, reject){
$.get(url, function(data){ //jquery async method
resolve(data);
})
.fail(function(){
reject();
})
})
}
get('users.all').then(
function(users){
app.users = users;
},
function(){
console.log("users not found")
}
})
var usersPromise = get('users.all'); 
var postsPromise = get('posts.everyone');
Promise.all([usersPromise, postsPromise]).then(
function(results){
controller.users = results[0];
controller.posts = results[1]
},
function(){
console.log(users and posts not found)
}
);
get('users.all').then(function(usersString) {
//returns the statement to the next .then in the promise
return JSON.parse(usersString);
})
.then(function(users){
//using the previously parsed information here
app.users = users;
})

Generators

function *foo(){ 
yield 1;
yield 2;
return 3;
}
var gen = foo();
gen.next(); //returns {value: 1, done:false}
gen.next(); // returns {value: 2, done: false} //there are no more //yield statements, so done now equals true
gen.next(); // returns {value: 3, done: true}
//there are no more values so it returns undefined
gen.next(); // returns {value: undefined, done: true}
function *foo(){ 
yield 1 yield 2 return 3
}
for (var val of foo()){
console.log(val)
} // 1, 2
function *foo(x){ 
var y = 2 * (yield(x+1));
var z = yield (y/3);
return (x + y + z);
}
var gen = foo(5)
console.log(gen.next()); // {value: 6, done: false}
console.log(gen.next(12)); // {value: 8, done: false}
console.log(gen.next(13)); // {value: 42, done: true}
var gen = foo(5) sets the initial x === 5
function *foo(5) { 
var y = 2 * (yield(5+1));
}
var y = 2 * 6 // the first next executes yield(5+1), which returns {value: 6, done: false}
function *foo(5){ 
y = 2*12 //y = 24
var z = (24/3) // y is applied and yield(24/3) returns {value: 8,
//done: false}
}
var x = 5 
var y = 24
var z = 13
return x + y + z //{value:42, done: true }

Modules

file.js: 
export var test;
export function sqr(x){console.log(x*x)};
export var test3;
main.js:
import{test, test2} from 'file';
sqr(3); // 9
main.js:

import * as lib from 'file';
lib.sqr(3); // 9
function sqr(x){
console.log(x*x)
}
export {sqr as squareLog}
import {sqr as squareLog} from 'file'; 
squareLog(3); // 9

When can I use ES6 ???

--

--

Software Architect

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store