Created
May 4, 2019 01:52
-
-
Save jsEveryDay/f57003707f4d7953fe2b75e37dadd729 to your computer and use it in GitHub Desktop.
Async Await Examples
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
//read this first | |
// //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await | |
// The await expression causes async function execution to pause until a Promise | |
// is resolved, that is fulfilled or rejected, and to resume execution of the async | |
// function after fulfillment. When resumed, the value of the await expression is | |
// that of the fulfilled Promise. | |
// If the Promise is rejected, the await expression throws the rejected value. | |
// If the value of the expression following the await operator is not a Promise, | |
// it's converted to a resolved Promise. | |
for await (variable of iterable) { | |
statement | |
} | |
var asyncIterable = { | |
[Symbol.asyncIterator]() { | |
return { | |
i: 0, | |
next() { | |
if (this.i < 3) { | |
return Promise.resolve({ value: this.i++, done: false }); | |
} | |
return Promise.resolve({ done: true }); | |
} | |
}; | |
} | |
}; | |
(async function() { | |
for await (let num of asyncIterable) { | |
console.log(num); | |
} | |
})(); | |
// 0 | |
// 1 | |
// 2 | |
async function* asyncGenerator() { | |
var i = 0; | |
while (i < 3) { | |
yield i++; | |
} | |
} | |
(async function() { | |
for await (let num of asyncGenerator()) { | |
console.log(num); | |
} | |
})(); | |
// 0 | |
// 1 | |
// 2 | |
async function* streamAsyncIterator(stream) { | |
const reader = stream.getReader(); | |
try { | |
while (true) { | |
const { done, value } = await reader.read(); | |
if (done) { | |
return; | |
} | |
yield value; | |
} | |
} finally { | |
reader.releaseLock(); | |
} | |
} | |
// Fetches data from url and calculates response size using the async generator. | |
async function getResponseSize(url) { | |
const response = await fetch(url); | |
// Will hold the size of the response, in bytes. | |
let responseSize = 0; | |
// The for-await-of loop. Async iterates over each portion of the response. | |
for await (const chunk of streamAsyncIterator(response.body)) { | |
// Incrementing the total response length. | |
responseSize += chunk.length; | |
} | |
console.log(`Reponse Size: ${responseSize} bytes`); | |
// expected output: "Response Size: 1071472" | |
return responseSize; | |
} | |
getResponseSize('https://jsonplaceholder.typicode.com/photos'); | |
/// | |
///Start | |
function resolveAfter2Seconds(x) { | |
return new Promise(resolve => { | |
setTimeout(() => { | |
resolve(x); | |
}, 2000); | |
}); | |
}; | |
var add = async function(x) { // async function expression assigned to a variable | |
var a = await resolveAfter2Seconds(20); | |
var b = await resolveAfter2Seconds(30); | |
return x + a + b; | |
}; | |
add(10).then(v => { | |
console.log(v); // prints 60 after 4 seconds. | |
}); | |
(async function(x) { // async function expression used as an IIFE | |
var p_a = resolveAfter2Seconds(20); | |
var p_b = resolveAfter2Seconds(30); | |
return x + await p_a + await p_b; | |
})(10).then(v => { | |
console.log(v); // prints 60 after 2 seconds. | |
}); | |
//FINISH |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment