@kangax created a new interesting quiz, this time devoted to ES6 (aka ES2015). I found this quiz very interesting and quite hard (made myself 3 mistakes on first pass).
Here we go with the explanations:
(function(x, f = () => x) {| function promiseAll() { | |
| if (!arguments.length) { | |
| return Promise.resolve(null); | |
| } | |
| var args = arguments; | |
| if (args.length === 1 && Array.isArray(args[0])) { | |
| args = args[0]; | |
| } | |
| var count = 1; | |
| var total = args.length; |
@kangax created a new interesting quiz, this time devoted to ES6 (aka ES2015). I found this quiz very interesting and quite hard (made myself 3 mistakes on first pass).
Here we go with the explanations:
(function(x, f = () => x) {| //run this example function in chrome console to see async await in action | |
| async function asyncAwaitExample() { | |
| let tmp = 1; | |
| let value1 = await getValueFromApi(2000); | |
| let value2 = await getValueFromApi(4000); | |
| //this function is here just to simulate a server/api behavior | |
| //returns a random value after provided time(in ms) | |
| function getValueFromApi(t) { | |
| return new Promise((rs, rj) => { setTimeout(v => { console.log(`getvalue call ${tmp} ---- ${v}`); tmp++; rs(v + v) }, t, Math.random().toString(36).substring(7)) }).then(v => v + v); |