This file contains hidden or 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
| asyncGreet () { | |
| this.someThingAsync() | |
| .then(this.greet.bind(this)) | |
| } |
This file contains hidden or 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
| class Foo { | |
| constructor (name) { | |
| this.name = name | |
| } | |
| greet () { | |
| console.log('hello, this is ', this.name) | |
| } | |
| someThingAsync () { |
This file contains hidden or 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 Greeters = [] | |
| for (var i = 0 ; i < 10 ; i++) { | |
| Greeters.push(function () { return console.log(i) }) | |
| } | |
| Greeters[0]() // 10 | |
| Greeters[1]() // 10 | |
| Greeters[2]() // 10 |
This file contains hidden or 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
| typeof {} === 'object' // true | |
| typeof 'a' === 'string' // true | |
| typeof 1 === number // true | |
| // But.... | |
| typeof [] === 'object' // true |
This file contains hidden or 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
| // These are ok | |
| 'abc' === 'abc' // true | |
| 1 === 1 // true | |
| // These are not | |
| [1,2,3] === [1,2,3] // false | |
| {a: 1} === {a: 1} // false | |
| {} === {} // false |
This file contains hidden or 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 s = "bob" | |
| const replaced = s.replace('b', 'l') | |
| replaced === "lob" // first match only | |
| s === "bob" // original string is remained unchanged |
This file contains hidden or 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 demoPromise (userId) { | |
| return User | |
| .findById(userId) | |
| .then(user => Promise.all([user, doSomethingAsync(user)])) | |
| .then(([user, result]) => finalResult(user, result)); | |
| } |
This file contains hidden or 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
| import {sleep} from 'previousExample' | |
| async function slowDouble (i): Promise<number> { | |
| await sleep(1000) // non-blocking | |
| return 2 * i | |
| } | |
| async function main () { | |
| // slowDouble is async, therefore it returns a promise | |
| const result = await Promise.all([1, 2, 3].map(slowDouble)) |
This file contains hidden or 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 sleep (ms: number) { | |
| return new Promise(resolve => { | |
| setTimeout(resolve, ms) | |
| }) | |
| } | |
| async function main () { | |
| console.log('hello') | |
| await sleep(1000) // non-blocking | |
| console.log('good bye') |
This file contains hidden or 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
| // ----- Old school Promise-style ----- // | |
| function nope (): Promise<never> { | |
| return Promise.reject('lol no') | |
| } | |
| // ----- Hipster async way ----- // | |
| async function asyncNope (): Promise<never> { | |
| throw 'lol no' | |
| } |