Skip to content

Instantly share code, notes, and snippets.

View patarkf's full-sized avatar

Patrick Ferreira patarkf

View GitHub Profile
posts.forEach(post => {
promise = promise.then(db.insert(post));
});
let promise = Promise.resolve();
const posts = [{}, {}, {}];
posts.forEach(post => {
promise = promise.then(() => {
return db.insert(post);
});
});
promise.then(() => {
async function makeRequest() {
const response = await fetch('foo');
if (response.doestItNeedAnotherRequest) {
const secondResponse = await makeAnotherRequest(response);
console.log(secondResponse);
return secondResponse;
}
console.log(response);
function makeRequest() {
return fetch('foo')
.then(response => {
if (data.doesItNeedAnotherRequest) {
return makeAnotherRequest(response)
.then(secondResponse => {
console.log(secondResponse);
return secondResponse;
});
async function makeRequest() {
try {
const result = JSON.parse(await fetch('foo'));
console.log(result);
} catch (error) {
console.log(error);
}
}
function makeRequest() {
try {
fetch('foo')
.then(response => {
const result = JSON.parse(response);
console.log(result);
})
.catch(error => {
console.log(error);
async function myAsyncFunction() {
try {
const result = await somethingThatReturnsAPromise();
console.log(result);
} catch (error) {
console.log(error);
}
}
async function myAsyncFunction() {
const result = await somethingThatReturnsAPromise();
console.log(result);
}
async function getFromGitHub() {
try {
const userName = 'patarkf';
const url = 'https://api.github.com/users';
const reposResponse = await fetch(`${url}/${userName}/repos`);
const userRepos = await reposResponse.json();
console.log(userRepos);
} catch (error) {
console.log(error);
@patarkf
patarkf / nodejs-promises-example.js
Last active June 12, 2017 14:43
Promise request example
function getFromGitHub() {
const userName = 'patarkf';
const url = 'https://api.github.com/users';
fetch(`${url}/${userName}/repos`)
.then(reposResponse => {
return reposResponse.json();
})
.then(userRepos => {
console.log(userRepos);