(async () => {
let response = await fetch('/article/promise-chaining/user.json');
let user = await response.json();
...
})();
// (BONUS CODE) await with promise
await new Promise(resolve => setTimeout(resolve, 1000))
console.log('Hello!') // 1 seconds after sended console.log
const foo = async () => {
await// do something
}
// OR
async function foo() {
await// do something
}
const mapUrl = 'https://';
(async (getUrl) => {
const response = await fetch(mapUrl);
const data = await response.json();
console.log(data);
})();
//Async function (JAVASCRIPT)
//async function have 1 main purpose:
//Being able to use 'await' in a function
function example() {
return await fetch('https://mysite.com/api'); //error
}
async function smartexample() {
return await fetch('https://mysite.com/api'); //mhmm!
}
const asyncExample = async () => {};
function myFunction() {
return Promise.resolve("Hello");
}
// async function example
async function f() {
console.log('Async function.');
return Promise.resolve(1);
}
f();