Skip to content

Instantly share code, notes, and snippets.

@edward-hsu-1994
Created July 10, 2016 16:08
Show Gist options
  • Save edward-hsu-1994/9eb28801db96e781e436aa1eed6ca52c to your computer and use it in GitHub Desktop.
Save edward-hsu-1994/9eb28801db96e781e436aa1eed6ca52c to your computer and use it in GitHub Desktop.
Typescript Async/Await Sample
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