O que é uma função assíncrona
// Old School Javascript Invoke
(async function() {
await someAsyncFunction();
})();
//New ES6 Javascript Invoke
(async () => {
await someAsyncFunction();
})();
//Example (async & await)
function delayResult() {
return new Promise(resolve => {
setTimeout(() => {
resolve(‘Done’);
}, 5000)
})
}
async function getResult() {
let result = await delayResult();
return result;
}
getResult();
//New Example
const data = async () => {
const got = await fetch('https://jsonplaceholder.typicode.com/todos/1');
console.log(await got.json())
}
data();
Tough Tuatara