async await api call javascript es6 code example
Example 1: async await javascript
function hello() {
return new Promise((resolve,reject) => {
setTimeout(() => {
resolve('I am adam.');
}, 2000);
});
}
async function async_msg() {
try {
let msg = await hello();
console.log(msg);
}
catch(e) {
console.log('Error!', e);
}
}
async_msg();
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;
}