Last active
January 17, 2018 15:13
-
-
Save embarq/fc25aeb9a475d07bd4eb72e4a8f02c34 to your computer and use it in GitHub Desktop.
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 setNameAsync(setName) { | |
| // how do you set a name after 2sec. pause ? | |
| setTimeout(setName, 2000); | |
| } | |
| const user = { | |
| name: 'Bill', | |
| setName: function(name) { | |
| this.name = name; | |
| console.log(this.name); | |
| } | |
| }; | |
| setNameAsync(user.setName); | |
| Promise.resolve(1) | |
| .then((x) => x + 1) | |
| .then((x) => { throw new Error('My Error') }) | |
| .catch(() => 1) | |
| .then((x) => x + 1) | |
| .then((x) => console.log(x)) | |
| .catch(console.error) | |
| const getRandomPromise = index => new Promise(resolve => { | |
| const timeout = Math.floor(Math.random() * 1000); | |
| setTimeout(() => { | |
| console.log(`[${ i }] Ended up after ${ timeout }`); | |
| resolve(); | |
| }, timeout); | |
| }); | |
| const promiseFactory = (_, i) => getRandomPromise(index); | |
| const promises = Array.from(new Array(10), promiseFactory); | |
| const getRandomPromise = index => new Promise((resolve, reject) => { | |
| const timeout = Math.floor(Math.random() * 1000); | |
| setTimeout(() => { | |
| // Randomly throw error | |
| if (timeout > 800) { | |
| reject(new Error(`[${ index }] Random Promise rejection after ${ timeout }`)); | |
| } else { | |
| resolve(`[${ index }] Ended up after ${ timeout }`); | |
| } | |
| }, timeout); | |
| }); | |
| const promiseFactory = (_, i) => getRandomPromise(i); | |
| const promises = Array.from(new Array(10), promiseFactory); | |
| Promise | |
| .all(promises) | |
| .then(x => console.log(x)); | |
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 foo = () => let a = 3; | |
| const foo = () => { a: 3 }; | |
| const bar = foo(); | |
| console.log(bar.a); | |
| for (const i = 0; i < 10; i++) { | |
| setTimeout(() => { console.log(i) },100) | |
| } | |
| var a = 2; | |
| { | |
| const a = 3; | |
| { | |
| console.log(a); | |
| } | |
| } | |
| const foo = function(a = (function(a) { return a == null ? {} : a })(a)) { | |
| console.log(a); | |
| } | |
| foo(null); | |
| foo(3); | |
| /** | |
| * @type {Array<{id: number, name: string, email: string}>} | |
| */ | |
| var users = [ | |
| { | |
| id: 1, | |
| name: "Robbi", | |
| email: "[email protected]" | |
| } | |
| // ... | |
| ]; | |
| users.reduce(function(map, user) { | |
| map[user.id] = user; | |
| return map; | |
| }, {}); | |
| const data = [ | |
| [ | |
| 1, | |
| 'one' | |
| ], | |
| [ | |
| 2, | |
| 'two', | |
| [ | |
| 4, | |
| 'four' | |
| ] | |
| ], | |
| [ | |
| 3, | |
| 'three' | |
| ] | |
| ]; | |
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 bar() { | |
| return foo; | |
| foo = 10; | |
| function foo() {} | |
| var foo = '11'; | |
| } | |
| alert(typeof bar()); | |
| (function(){ | |
| return typeof arguments; | |
| })(); | |
| var x = 1; | |
| if (function f(){}) { | |
| x += typeof f; | |
| } | |
| x; | |
| (function f(){ | |
| function f(){ return 1; } | |
| return f(); | |
| function f(){ return 2; } | |
| })(); | |
| (function() { | |
| if (true) { | |
| var a = 5; | |
| } | |
| alert(a); | |
| })(); | |
| function sum(a, b, c) { | |
| return a + b + c; | |
| } | |
| (function(a, b, c) { | |
| return a + b + c; | |
| }).call(context, [ arg1, arg2, arg3 ]); | |
| (function(a, b, c) { | |
| return a + b + c; | |
| }).apply(context, [ arg1, arg2, arg3 ]); | |
| (function(a, b, c) { | |
| return a + b + c; | |
| }).bind(context, [ arg1, arg2, arg3 ]); | |
| (function(a, b, c) { | |
| return a + b + c; | |
| })([ arg1, arg2, arg3 ]); | |
| for (var i = 0; i < 10; i++) { | |
| setTimeout(function() { | |
| console.log(i); | |
| }, i); | |
| } | |
| for (var i = 0; i < 10; i++) { | |
| setTimeout((function(ii) { | |
| return function() { | |
| console.log(ii) | |
| } | |
| })(i), i); | |
| } |
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
| "455" - 4; | |
| [, 'me', 42] | |
| !!!0 ? !!1 === !!'false' : 1 + '1' | |
| === () || = && | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment