how to write good async await code typescript code example
Example 1: async await function in typescript
function delay(milliseconds: number, count: number): Promise<number> {
return new Promise<number>(resolve => {
setTimeout(() => {
resolve(count);
}, milliseconds);
});
}
async function dramaticWelcome(): Promise<void> {
console.log("Hello");
for (let i = 0; i < 5; i++) {
const count: number = await delay(500, i);
console.log(count);
}
console.log("World!");
}
dramaticWelcome();
Example 2: javscript async await explained
function getJSON(){
return new Promise( function(resolve) {
axios.get('https://tutorialzine.com/misc/files/example.json')
.then( function(json) {
resolve(json);
});
});
}
async function getJSONAsync(){
let json = await axios.get('https://tutorialzine.com/misc/files/example.json');
return json;
}