Created
October 20, 2022 15:08
-
-
Save mscolnick/c3d3116e04c01b96bcd3edcb1fc3a773 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 exampleOne() { | |
console.log('One'); | |
return Promise.resolve() | |
.then(() => console.log('Two')) | |
.catch(() => console.log('Three')) | |
.finally(() => console.log('Four')); | |
} | |
function exampleTwo() { | |
console.log('One'); | |
return Promise.resolve() | |
.then(() => console.log('Two')) | |
.catch(() => console.log('Three')) | |
.finally(() => console.log('Four')) | |
.then(() => console.log('Five')) | |
.catch(() => console.log('Six')) | |
.finally(() => console.log('Seven')); | |
} | |
function exampleThree() { | |
console.log('One'); | |
return Promise.reject() | |
.then(() => console.log('Two')) | |
.catch(() => console.log('Three')) | |
.finally(() => console.log('Four')) | |
.then(() => console.log('Five')) | |
.catch(() => console.log('Six')) | |
.finally(() => console.log('Seven')); | |
} | |
function exampleFour() { | |
console.log('One'); | |
return new Promise((resolve, reject) => { | |
console.log('Two'); | |
resolve(null); | |
console.log('Three'); | |
reject(new Error('Four')); | |
console.log('Five'); | |
}); | |
} | |
function exampleFive() { | |
console.log('One'); | |
return new Promise((resolve, reject) => { | |
console.log('Two'); | |
new Promise((resolve, reject) => { | |
resolve("Three"); | |
reject(new Error('Four')); | |
}).then((val) => { | |
console.log(val) | |
reject(val); | |
}); | |
}).catch((val) => console.log(val)); | |
} | |
function exampleSix() { | |
console.log('One'); | |
Promise.resolve().then(() => console.log('Two')); | |
console.log('Three'); | |
return; | |
} | |
async function exampleSeven() { | |
console.log('One'); | |
await Promise.resolve().then(() => console.log('Two')); | |
console.log('Three'); | |
return; | |
} | |
const examples = [exampleOne, exampleTwo, exampleThree, exampleFour, exampleFive, exampleSix, exampleSeven]; | |
(async function main() { | |
for (const example of examples) { | |
console.log(); | |
console.log(example.name); | |
console.log('-------'); | |
await example(); | |
} | |
})(); | |
// https://replit.com/@MylesScolnick/promises?v=1#index.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment