Javascript promise, async function and await
How to use async functionality in Javascript?
An async function returns a promise. If this promise has done, the await function will be execute. A await function can only exists in async function.
Aysnc loading Example in Javascript:
function loadHeader() {
return getAllContent().then(function(content) {
return content[0].header;
}).catch(function(err) {
return {
content: 'loading error'
};
});
}
New async promise await developing Example in Javascript:async function getHeader() {
try {
let content = await getAllContent();
return content[0].header;
} catch (err) {
return {
content: 'loading error'
};
}
}