Skip to content

Instantly share code, notes, and snippets.

@Ibro
Last active December 27, 2017 19:17
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/7ab993c8766dc70b099bde8b89f0e73a to your computer and use it in GitHub Desktop.
Save Ibro/7ab993c8766dc70b099bde8b89f0e73a to your computer and use it in GitHub Desktop.
JavaScript Promises, road to async/await - https://codingblast.com/javascript-promise-async-await
function someHelperFunctionThatReturnsPromise(value, error) {
console.log('Inside someHelperFunctionThatReturnsPromise()');
return new Promise(function(resolve, reject) {
if (value) {
resolve(value);
} else {
reject(error);
}
});
}
async function someAsyncFunctionThatAwaitsPromise(someValue) {
console.log('Inside someAsyncFunctionThatAwaitsPromise()');
let value = await someHelperFunctionThatReturnsPromise(someValue);
return value;
}
async function anotherAsyncFunctionThatAwaitsAsyncFunction() {
console.log('Inside anotherAsyncFunctionThatAwaitsAsyncFunction()');
let value = await someAsyncFunctionThatAwaitsPromise('CodingBlast');
console.log(value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment