Last active
May 29, 2023 16:09
-
-
Save estevam5s/a433c66de377b2b85800a995076071bd to your computer and use it in GitHub Desktop.
promises.js
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
Promise.all([ | |
api.get('/users/diego3g'), | |
api.get('/phones/diego3g'), | |
]).then([user, phones] => { | |
console.log(user); | |
console.log(phones); | |
}); |
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
api.get('/users/diego3g').then(user => { | |
api.get(`/groups/${user.id}`).then(groups => { | |
groups.map(group => { | |
api.get(`/group/${group.id}`).then(groupInfo => { | |
console.log(groupInfo); | |
}); | |
}) | |
}); | |
}); |
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
// Modelo antigo (ES6) | |
function fetchUser(user) { | |
api.get(`/users/${user}`).then(response => { | |
console.log(response); | |
}); | |
} | |
// Novo modelo (ES8) | |
async function fetchUser(user) { | |
const response = await api.get(`/users/${user}`); | |
console.log(response); | |
} |
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
const divDelayed = (a, b) => new Promise((resolve, reject) => { | |
setTimeout(() => { | |
if (b == 0) reject('O valor de B não pode ser zero!'); | |
resolve(a / b); | |
}, 2000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment