This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ES6 syntax | |
function load() { | |
return Promise.all([foo(), bar()]) | |
.then(console.log); | |
} | |
load(); | |
// ES7 syntax | |
async function load() { | |
let a = await foo(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo () { | |
console.log( this.a ); | |
} | |
// #1: Default binding | |
var a = 'bar'; | |
// [call-site: global] | |
foo(); // 'bar' or undefined (strict mode) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); |