Skip to content

Instantly share code, notes, and snippets.

@toriaezunama
Last active August 15, 2019 22:37
Show Gist options
  • Save toriaezunama/0c6b4f648c4469e418c153407e308385 to your computer and use it in GitHub Desktop.
Save toriaezunama/0c6b4f648c4469e418c153407e308385 to your computer and use it in GitHub Desktop.
Promise and async Qs
// 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');
});
// 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
})
}
// 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];
}
@toriaezunama
Copy link
Author

Error 1
Success 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment