Skip to content

Instantly share code, notes, and snippets.

@MarwanShehata
Created January 26, 2022 21:16
Show Gist options
  • Save MarwanShehata/484879cba4337bc128e6e05bc8cfc024 to your computer and use it in GitHub Desktop.
Save MarwanShehata/484879cba4337bc128e6e05bc8cfc024 to your computer and use it in GitHub Desktop.
// https://youtu.be/D-JIYZhEqv8
// The Async Features You’ve Been Awaiting - Yael Hermon
function foo() {
getSomeAsyncStuff()
.then(getSomeMoreAsyncStuff)
.then(getSomeMoreAsyncStuff)
.then(result=> console.log(result))
.catch((err) => console.log(err););
}
// But what happens if we need to save a reference from one async function just to do a final calc with another async function...then we'll have to start indenting and back to callback hell and we need a better solution
//////////////
// Again, we hand our error that come from promises with .catch
// but our synchronous error are caught with a try & catch blocks. so we are again handling our errors in two places in two different ways and that isn't fun at all
function foo() {
try {
runSyncCodeThatMightThrow();
getSomeAsyncStuffThatMightThrow()
.then(result=>console.log(result))
.catch(error=>console.log(error))
} catch (error) {
console.log(error);
}
}
foo();
//////
// Solution
async function foo() {
const result = await getSomeAsyncStuff();
return `hello ${result}`;
}
foo();
thisFunctionDoesntWaitForFoo();
/////
// Solution #2
async function foo() {
try {
runSyncCodeThatMightThrow();
const result= await getSomeAsyncStuff()
console.log(result);
} catch (error) {
console.log(error);
}
}
///
function foo() {
if (syncCondition) {
asyncCondition()
.then(isConditionTrue=>{
if (isConditionTrue) {
// do code
}})
.catch(error=>{
// handle error
})
}
}
// VS
// easier to maintain
// you can put it inside if, or put it as a value in an object litteral
async function foo() {
try {
if (syncCondition && await asyncCondition()) {
// do code
}
} catch (error) {
// do code
}
}
// https://imgur.com/a/CUPGH56
/////////////////////////////////////////
// But we need to be careful
async function foo() {
const firstResult = await getStuff();
const secondResult = await getStuffUnrelated();
console.log(firstResult+secondResult);
}
foo();
// above here we should call them concurrently and save time
// BAD PRACTICE ☝
async function foo() {
const a = getStuff();
const b = getStuffUnrelated();
const firstResult = await a;
const secondResult = await b;
console.log(firstResult+secondResult);
}
// Above are handled concurrently because it already starts but we are waiting for them sequentially
// so if a gets rejected, we'll never handle b. and if a got resolved and b doesn't resolve we'll be wasting our time
////////////
async function foo() {
const a = getStuff();
const b = getStuffUnrelated();
const [firstResult, secondResult]= await Promise.all([a, b]);
console.log(firstResult+secondResult);
}
// Good Practice 👆 if a or b didn't handle, we won't get a Handle Rejection Error. also both are handled concurrently.
/////
const iterable = {
[Symbol.iterator](){
let i=0;
let iterator = {
next() {
const iteratorResult ={
value: i*i,
done: false,
};
i++;
return iteratorResult;
}
};
return iterator;
}
}
function* generatorFunc() {
yield `Hello`;
yield `world`;
}
const generator = generatorFunc();
generator.next();
generator.next();
generator.next();
////////////////
// Streams:-
// 1- readable
// 2- writable
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment