Skip to content

Instantly share code, notes, and snippets.

@Ibro
Last active December 27, 2017 19:17
Embed
What would you like to do?
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