by Sebastian Wennemers
(@S7nW7s)
Architect SAPUI5 Flexibility
Don't call us, we call you (Inversion of Control)
Event driven
Compose declarative Event streams
(instead of callback hell)
iterators & generators
for of loops
async - await
async iterators & generators
async for of loops
var someIterableObject = {
[Symbol.iterator]: function(){
var i = 1;
return {
next: function() {
if (i <= 42) {
return {done: false, value: "Chunk " + i++ };
} else {
return {done: true};
}
}
}
}
}
next(): Chunk 1 Chunk 2 Chunk 3 ... Chunk 42 DONE
var someIterableObject = {
getChunksGeneratorFunction: function* (){ //returns an iterator
for (var i=1; i <= 42; i++) {
yield "Chunk " + i; //next value
}
/* can be implicit */return; //done
}
}
Yielding: Chunk 1 Chunk 2 Chunk 3 ... Chunk 42 DONE
var iterator = someIterableObject[Symbol.iterator]();
for (var result = iterator.next(); !result.done; result = iterator.next()) {
console.log(result.value);
}
for (var value of someIterableObject.getChunksGeneratorFunction()){
console.log(value)
}
function doSomethingAsync(){
//make some backend call, etc.
//return a promise
return Promise.resolve(5)
}
async function doAsyncStuff(url) {
try {
var iResolvedValue = await doSomethingAsync();
return iResolvedValue + 1; //will be wrapped into promise as it is an async function
}
catch (error) {
console.log("ERROR: " + error.stack);
}
}
var someIterableObject = {
[Symbol.asyncIterator]: function(){
var i = 1;
return {
next: function() {
if (i <= 42) {
return Promise.resolve({done: false, value: "Chunk " + i++ });
} else {
return Promise.resolve({done: true});
}
}
}
}
}
function someAsyncOperation() {
/* something async */
Promise.resolve(1);
}
var someIterableObject = {
getAsyncChunksGeneratorFunction: async function* (){ //returns an async iterator
for (var i=1; i <= 42; i++) {
var value = await someAsyncOperation(); //next() return now with a promise
yield "Chunk " + i + value; //resolves next value after someAsyncOperation
}
/* can be implicit */return; //done
}
}
var iterator = someIterableObject[Symbol.asyncIterator]();
iterator.next().then(function(result){
if (!result.done){
console.log(result.value);
}
});
for async (var value of someIterableObject.getAsyncChunksGeneratorFunction()){
console.log(value)
}