Last active
August 15, 2019 22:37
-
-
Save toriaezunama/0c6b4f648c4469e418c153407e308385 to your computer and use it in GitHub Desktop.
Promise and async Qs
This file contains 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
// Question: What is the result of running the following? | |
function job() { | |
return new Promise(function(resolve, reject) { | |
reject(); | |
}); | |
} | |
let promise = job(); | |
promise | |
.then(function() { | |
console.log('Success 1'); | |
}) | |
.then(function() { | |
console.log('Success 2'); | |
}) | |
.then(function() { | |
console.log('Success 3'); | |
}) | |
.catch(function() { | |
console.log('Error 1'); | |
}) | |
.then(function() { | |
console.log('Success 4'); | |
}); |
This file contains 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
// Let’s imagine we have renderUI function that draws users and comments . | |
// This function needs to be run AFTER both getUsers and getUsersInfo requests have returned data | |
// Questions: How we can do this? | |
function renderUI(users, comments) { | |
console.log('Rendering ....'); | |
console.log('Users', users); | |
console.log('Comments', comments); | |
} | |
function getUsers() { | |
return new Promise((resolve, reject) => { | |
// some request | |
}) | |
} | |
function getComments() { | |
return new Promise((resolve, reject) => { | |
// some request | |
}) | |
} |
This file contains 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
// Question: What is wrong with the following? | |
function getUsers() | |
return new Promise((resolve, reject) => { | |
// some request | |
}) | |
} | |
function getComments() { | |
return new Promise((resolve, reject) => { | |
// some request | |
}) | |
} | |
async function getData() { | |
const users = getUsers(); | |
const comments = getComments(); | |
return [users, comments]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Error 1
Success 4