Skip to content

Instantly share code, notes, and snippets.

@jbardon
jbardon / callback-simple.js
Created November 13, 2018 11:24
#11 You know nothing (medium)
// Please call 'eatPizza' once you've finished your work
orderPizza(eatPizza);
function orderPizza(callback) {
// You don't know what's going on here!
callback(); // <- Hope it's this
}
function eatPizza() {
console.log('Miam');
@jbardon
jbardon / async-good.js
Last active November 14, 2018 14:13
#10 You know nothing (Medium)
async function load() {
let fooPromise = foo();
let barPromise = bar();
// foo and bar are executed before Promise.all
let results = await Promise.all([fooPromise, barPromise]);
console.log(results);
}
load();
@jbardon
jbardon / async-bad.js
Last active November 14, 2018 14:12
#9 You know nothing (Medium)
// ES6 syntax
function load() {
return Promise.all([foo(), bar()])
.then(console.log);
}
load();
// ES7 syntax
async function load() {
let a = await foo();
@jbardon
jbardon / promise-chain-detail.js
Last active November 14, 2018 11:40
#8 You know nothing (Medium)
const cookPromise = cookPizza();
const packPromise = cookPromise.then(function(pizza) {
return pack(pizza); // Returns a promise stored in packPromise
});
const deliverPromise = packPromise.then(function (packedPizza) { // value from pack(pizza)
return deliver(packedPizza);
});
@jbardon
jbardon / promise-multiple-then.js
Last active November 14, 2018 12:25
#7 You know nothing (Medium)
// Function executed even if there are no then or catch
let promise = Promise.resolve('Pizza');
// Add callbacks later, called depending on the promise status
promise.then(youEatOneSlice);
promise.then(yourFriendEatOneSlice);
promise.then(result => console.log(result)); // 'Pizza'
// Promise is an object (with at least a then function: it's a thenable object)
console.log(promise); // { state: 'fulfilled', value: 'Pizza'}
@jbardon
jbardon / promise-sample.js
Last active November 13, 2018 11:37
#6 You know nothing (Medium)
let promise = orderPizza(); // <- No callback
// Subscribes to the promise
promise.then(eatPizza); // Fulfilled promise
promise.catch(stillHungry); // Rejected promise
function orderPizza() {
return Promise.resolve(); // <- returns the promise
}
@jbardon
jbardon / this-issues.js
Created November 12, 2018 13:06
#5 You know nothing (Medium)
// 1- Call-site issue
const o = { a: 'bar', foo };
callback(o.foo); // undefined
function callback(func){
func(); // [call-site: callback]
}
// 2- Default binding isn't lexical binding
var a = 'foo';
@jbardon
jbardon / this-rules.js
Created November 12, 2018 13:04
#4 You know nothing (Medium)
// #2: Implicit binding
const o2 = { a: 'o2', foo };
const o1 = { a: 'o1', o2 };
o1.o2.foo(); // [call-site: o2] 'o2'
// #3: Explicit binding
const o = { a: 'bar' };
foo.call(o); // [call-site: o] 'bar'
@jbardon
jbardon / this-default.js
Last active November 13, 2018 09:46
#3 You know nothing (Medium)
function foo () {
console.log( this.a );
}
// #1: Default binding
var a = 'bar';
// [call-site: global]
foo(); // 'bar' or undefined (strict mode)
@jbardon
jbardon / map-class.js
Last active November 12, 2018 13:02
#2 You know nothing (Medium)
var map = new Map();
map.set(x, 'foo');
map.set(y, 'bar');
console.log(map.get(x), map.get(y)); // 'foo', 'bar'
// undefined, undefined
console.log(map.get({ id: 1 }, map.get({ id: 2});