Skip to content

Instantly share code, notes, and snippets.

@Ibro
Last active Dec 24, 2017
Embed
What would you like to do?
JavaScript Promises, road to async/await - https://codingblast.com/javascript-promise-async-await
function someFunction(value, error) {
return new Promise((resolve, reject) => {
if (value) {
resolve(13);
} else {
reject(error);
}
});
}
function fetchPost(id) {
return fetch('https://jsonplaceholder.typicode.com/posts/' + id)
.then(
response => response.json(),
error => error)
.then(function(post) {
return post;
});
}
function mainFunctionSync() {
someFunction('value').then(
id => {
fetchPost(id).then(
post => { console.log(post); },
error => {console.error(error); }
);
},
error => {
console.error(error);
}
);
}
mainFunctionSync();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment