Skip to content

Instantly share code, notes, and snippets.

View priteshjha4u's full-sized avatar
🎯
Focusing

Pritesh Kr Jha priteshjha4u

🎯
Focusing
View GitHub Profile
@priteshjha4u
priteshjha4u / promiseAll.js
Created August 8, 2019 11:04
Promise.all implementation in vanilla JavaScript
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's ES6 quiz, explained

@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:

Question 1:
(function(x, f = () => x) {
@priteshjha4u
priteshjha4u / asyncawait-example.js
Last active July 4, 2019 13:42
async await es2017 simple example
//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);