Created
August 29, 2018 19:12
-
-
Save devrsantos/7f04c3c593ebef3106416404e61e94b1 to your computer and use it in GitHub Desktop.
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 fetch = require('node-fetch'); | |
const defer = new Promise((resolve, reject) => { | |
setTimeout(() => { | |
if (true) { | |
resolve('Hello! It works!'); | |
} else { | |
reject('Error'); | |
} | |
}, 2000); | |
}); | |
defer.then(data => console.log(data)); | |
const posts = async function getPosts() { | |
try { | |
const response = await fetch('https://willianjusten.com.br/search.json'); | |
const data = await response.json(); | |
return data.map(post => { | |
console.log(post.title); | |
}); | |
} catch (err) { | |
console.error('Noooooo, we got an error!', err); | |
} | |
} | |
posts(); | |
async function getUsers(users) { | |
const promise = users.map(user => fetch(`https://api.github.com/users/${user}`).then(r => r.json())); | |
const people = await Promise.all(promise); | |
console.log(people.map(p => p.company)) | |
} | |
getUsers(['willianjusten', 'guilouro', 'lhbzr']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment