Last active
December 24, 2017 13:49
-
-
Save Ibro/ffd86c8d09f5362131823b50faf20da2 to your computer and use it in GitHub Desktop.
JavaScript Promises, road to async/await - https://codingblast.com/javascript-promise-async-await
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
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