Skip to content

Instantly share code, notes, and snippets.

@craftfortress
Created July 26, 2016 10:48
Show Gist options
  • Save craftfortress/539151166c18e7c53b4dda7542c9bb90 to your computer and use it in GitHub Desktop.
Save craftfortress/539151166c18e7c53b4dda7542c9bb90 to your computer and use it in GitHub Desktop.
//ES6 Examples
//ES6 Promises
function leFunction(){
return new Promise( (resolve, reject) => {
if( (Math.random()*100 ) > 10)
resolve("ok");
reject("Promise.all Reject" );
} );
}
Promise.all(
[leFunction(), leFunction(), leFunction()
]).then( (data) => { console.log("Promise All : " + data) }).catch( (data) => { console.log(data) });
//Reflection
var a= { a:1,b:2,c:3}
console.log("Reflection : " + Reflect.ownKeys(a) );
// Proxy
var awesome = { wat: 1 };
var proxy = new Proxy(awesome, {
get (receiver, name ){
return "awesome";
}
});
proxy.wat = 3;
console.log("new Proxy(x,y) : " + awesome.wat);
// Curreny Formatting
var moneyFormat = new Intl.NumberFormat("en-US", { style : "currency" , currency: "USD"});
console.log("Currency Number Format : " + moneyFormat.format(102301200) );
//Math Trunc
console.log("Math Trunc : " + Math.trunc(124124.12123));
//floats
console.log("Decimal toFixed : " + (0.1 + 0.2).toFixed(2) );
//isNan
console.log("isNaN " + isNaN(123));
//includes
console.log("string.includes(x,1) " + "test".includes("t"));
//Find first element
console.log("array.find(x => x >4 ) (first element) " + [1,2,3,4,5,6].find(x => x >4).toString() )
//assign
var a = { w: 1, s: 2};
var b = { y:3, z: 4};
var x = {};
Object.assign(x, a, b);
console.log("Object.assign(a,b,c) : " + (x.z).toString() );
//set
var set = new Set();
set.add("hello");
for (var x of set.values()){
console.log("set.add(x) " + x);
}
//map
var map = new Map();
map.set(x,123);
for (let [z,x] of map.entries() ){
console.log("let z of map.values() " + z + x);
}
//Generator
let generator = {
*[Symbol.iterator](){
let val1 = 0;
for (;;){
[val1] = [val1+10]
yield val1
}
}
}
for(let x of generator){
if(x >40)
break
console.log("Generator " + x);
}
// Generator Functions
function* generatorFunction(x,y){
while(x<5){
yield x
x +=1
}
}
for (let z of generatorFunction(1,0) ){
console.log("Generator Function " + z);
}
let array = [ ...generatorFunction(1,5)];
console.log(" let array = [ ...generatorFunction(1,5)] : " + array.toString());
//Async control-flow driver
/*
function async (proc, ...params) { var iterator = proc(...params) return new Promise((resolve, reject) => { let loop = (value) => { let result try { result = iterator.next(value) } catch (err) { reject(err) } if (result.done) resolve(result.value) else if ( typeof result.value === "object" && typeof result.value.then === "function") result.value.then((value) => { loop(value) }, (err) => { reject(err) }) else loop(result.value) } loop() }) }
function asynch_builder(text,after){
return new Promise((resolve,reject) => {
setTimeout(() => resolve(text),after )
})
}
async(function* (greeting){
let foo = yield asynch_builder("wat", 1000);
return '${greeting} ${foo}'
}, "hello").then((msg)=> { console.log("result :" , msg )
})
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment