Created
October 18, 2020 20:25
-
-
Save haltaction/a0d761abd41df89456ddc80436b9d08b 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
// 0 - synchronios call | |
function runSync() { | |
const end = Date.now() + 3000; | |
while (Date.now() < end) { | |
const doSomethingHeavyInJavaScript = Math.random; | |
} | |
return 'task done'; | |
} | |
console.log('1'); | |
console.log(runSync()); | |
console.log('2'); | |
// async | |
// 1 - callback | |
function runAsync(data, callback) { | |
setTimeout(() => callback('task done'), 3000); | |
} | |
console.log('1'); | |
runAsync({}, (result) => console.log(result)); | |
console.log('2'); | |
// 3.5 callback hell | |
// todo | |
// 2 - promise | |
let promise = new Promise( | |
(resolve, reject) => { | |
setTimeout(() => resolve('ok'), 3000); | |
} | |
) | |
let promiseReject = new Promise( | |
(resolve, reject) => { | |
setTimeout(() => reject('something not worked'), 3000); | |
} | |
) | |
console.log('1'); | |
promise | |
.then( | |
result => { | |
console.log(result); | |
} | |
) | |
.then(() => promiseReject) | |
.catch(error => { | |
console.log(error); | |
throw error; | |
}) | |
console.log('2'); | |
// promise chaining | |
// todo | |
// 3 - async/await | |
let promise = new Promise( | |
(resolve, reject) => { | |
setTimeout(() => resolve('ok'), 3000); | |
} | |
) | |
async function run() { | |
console.log('1'); | |
let result = await promise; | |
console.log(result); | |
console.log('2'); | |
} | |
run(); | |
// 4 - rxjs | |
// todo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment