Created
July 10, 2016 16:08
-
-
Save edward-hsu-1994/9eb28801db96e781e436aa1eed6ca52c to your computer and use it in GitHub Desktop.
Typescript Async/Await Sample
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
async function get(url) : Promise<string> { | |
return new Promise<string>((resolve, reject) => { | |
var xhr = new XMLHttpRequest(); | |
xhr.onreadystatechange = function (event) { | |
if (xhr.readyState !== 4) return; | |
if (xhr.status >= 200 && xhr.status < 300) { | |
resolve(xhr.responseText);//OK | |
} else { | |
reject(xhr.statusText);//Error | |
} | |
}; | |
xhr.open('GET', url, true);//Async | |
xhr.send(); | |
}); | |
} | |
async function main() { | |
var indexPage = await get("index.html"); | |
console.log(indexPage); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment