Skip to content

Instantly share code, notes, and snippets.

@Ibro
Last active December 24, 2017 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ibro/333c2c9e69b3ab4ab41259fd49319a0c to your computer and use it in GitHub Desktop.
Save Ibro/333c2c9e69b3ab4ab41259fd49319a0c to your computer and use it in GitHub Desktop.
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;
});
}
async function mainFunction() {
try {
let id = await someFunction('Some value');
let post = await fetchPost(id);
console.log(post);
} catch (error) {
console.error(error);
}
}
mainFunction();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment