Created
November 24, 2015 22:44
-
-
Save justin808/f8e493cfa8eddf885ec2 to your computer and use it in GitHub Desktop.
Example of async/await: http://forum.shakacode.com/t/promises-tips-and-articles/302/5?u=justin
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
var longFn = function(x) { | |
return new Promise(function(res, rej) { | |
if (x) { | |
setTimeout(res(x), 1000); | |
} else { | |
rej("ERROR from not passing in a value to longFn"); | |
} | |
}); | |
}; | |
async function f(inputToAsync) { | |
console.log("f() starting with param: ", inputToAsync); | |
let result; | |
try { | |
result = await longFn(inputToAsync); | |
} catch (err) { | |
console.log("THROWING err: " + err); | |
throw err; | |
} | |
return "RETURN VALUE FROM ASYNC function: " + inputToAsync; | |
} | |
var p = f("INPUT TO ASYNC FUNCTION"); | |
console.log("1: return value of async function is ", p); | |
p.then( | |
res => console.log("1: FINISHED: got back: " + res) | |
).catch( | |
err => console.log("1: GOT ERROR: err") | |
); | |
var p = f(false); | |
console.log("2: return value of async function is ", p); | |
p.then( | |
res => console.log("2: FINISHED: got back: " + res) | |
).catch( | |
err => console.log("2: GOT ERROR: err") | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment