Skip to content

Instantly share code, notes, and snippets.

@devrsantos
Created August 29, 2018 19:12
Show Gist options
  • Save devrsantos/7f04c3c593ebef3106416404e61e94b1 to your computer and use it in GitHub Desktop.
Save devrsantos/7f04c3c593ebef3106416404e61e94b1 to your computer and use it in GitHub Desktop.
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