- Overview of nonlinear control flow in javascript
- Async function
- Syntax(function, method, async arrow function expression)
- Runtime -> Diagram how it works…
- Examples
- Use Symbols.Async
- Using await
- Using promises
- Promise all
- Promise race
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
var getPromise = (value, delay) => { | |
return new Promise(resolve => { | |
setTimeout(() => resolve(value), delay); | |
}); | |
}; | |
var promise1 = getPromise('value-1', 150); | |
var promise2 = getPromise('value-2', 250); | |
var promise3 = getPromise('value-3', 50); |
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
var getPromise = (value, delay) => { | |
return new Promise(resolve => { | |
setTimeout(() => resolve(value), delay); | |
}); | |
}; | |
var promise1 = getPromise('value-1', 350); | |
var promise2 = getPromise('value-2', 250); | |
var promise3 = getPromise('value-3', 150); |
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
async function foo () { | |
const result = await fetch("https://randomuser.me/api/?results=10", { method: "GET" }); | |
return result; | |
} | |
async function bar() { | |
console.log(await foo()); | |
}; | |
bar(); |
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 SuperClass { | |
constructor() { | |
print('SuperClass constructor') | |
} | |
} | |
let ProxiedSuperClass = new Proxy(SuperClass, {}); | |
class A extends ProxiedSuperClass { | |
constructor() { |
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
async function foo () { | |
const result = await fetch("https://randomuser.me/api/?results=10", { method: "GET" }); | |
return result; | |
} | |
const boo = async function () { | |
return await fetch("https://randomuser.me/api/?results=10", { method: "GET" }); | |
}; | |
foo(); |
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 = async function () { | |
console.log('before await'); | |
const value = await 'boo'; | |
console.log('after await'); | |
return value; | |
}; | |
console.log('before async invocation'); | |
foo(); | |
console.log('after async invocation'); |
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
var foo = async function () { | |
try { | |
await Promise.reject('error in promise'); | |
} catch (e) { | |
console.log('caught error:', e); | |
} | |
}; | |
foo().then( | |
result => console.log('success-', 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
async function foo(args) { | |
console.log('before await'); | |
const value = await 'b'; | |
console.log('after await', value); | |
return value; | |
}; | |
//Simplified version of async foo function | |
function foo() { | |
let resolve; |
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
//TODO: Add console.log results | |
async function foo () { | |
console.log('foo-start'); | |
const result = await 'a'; | |
console.log('foo-end'); | |
return result; | |
} | |
async function boo() { |