xxxxxxxxxx
(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
xxxxxxxxxx
const foo = async () => {
await// do something
}
// OR
async function foo() {
await// do something
}
xxxxxxxxxx
//ORIGINAL
//each .then() returns a promisse and can be chained. The .then() will run after the previous one has finished. It emulates an async/await behavior.
fetch('https://jsonplaceholder.typicode.com/users')//fetch the content
.then((response) => response.json())//then use that content and convert it to a javascript object
.then(users => {
const firstUser = users[0];
console.log(firstUser);
return fetch('https://jsonplaceholder.typicode.com/posts?userId=' + firstUser.id);
})//then return the content from posts related to that user ID
.then((response) => response.json())//then use that content and convert it to a javascript object
.then(posts => console.log(posts));//then log the result
//ASYNC/AWAIT
const myAsyncFunction = async () => {//define that this will be asynchronous
const usersResponse = await fetch('https://jsonplaceholder.typicode.com/users');//wait for the fecth result and put it in the variable
const users = await usersResponse.json();//wait for the previous item to be done, transform it into a javascript object and put it in the variable
const secondUser = users[1];
console.log(secondUser);//then log the result
const postsResponse = await fetch('https://jsonplaceholder.typicode.com/posts?userId=' + secondUser.id);//wait for the previous item to be done, wait for the fecth result and put it in the variable
const posts = await postsResponse.json();//wait for the previous item to be done, transform it into a javascript object and put it in the variable
console.log(posts);//then log the result
}
myAsyncFunction();
xxxxxxxxxx
//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!
}
xxxxxxxxxx
async function getData() {
const [error, response] ?= await fetch("https://api.example.com/data");
if (error) return handleError(error);
return response;
}
xxxxxxxxxx
let line =
"Quisque vel rutrum lacus. Morbi sed finibus elit, in dictum nulla. Ut ac fermentum lectus, at molestie leo. Etiam ligula eros, placerat ut mattis at, aliquam ac lectus. Vivamus a ultrices odio. Sed vulputate dapibus sem sit amet imperdiet. Interdum et malesuada fames ac ante ipsum primis in faucibus. Morbi mollis felis pellentesque, bibendum sapien eu, bibendum nunc. Vivamus faucibus, nunc eget dapibus consectetur, diam dolor iaculis mauris, at tristique dui quam at neque. Maecenas ligula ex, ultricies dignissim dui eget, suscipit elementum tellus. Aliquam erat volutpat. Mauris semper quam vitae faucibus accumsan. Duis laoreet auctor tellus, pulvinar elementum est pellentesque ornare. Morbi bibendum scelerisque luctus. Phasellus elementum dignissim sapien sed dignissim.Quisque vel rutrum lacus. Morbi sed finibus elit, in dictum nulla. Ut ac fermentum lectus, at molestie leo. Etiam ligula eros, placerat ut mattis at, aliquam ac lectus. Vivamus a ultrices odio. Sed vulputate dapibus sem sit amet imperdiet. Interdum et malesuada fames ac ante ipsum primis in faucibus. Morbi mollis felis pellentesque, bibendum sapien eu, bibendum nunc. Vivamus faucibus, nunc eget dapibus consectetur, diam dolor iaculis mauris, at tristique dui quam at neque. Maecenas ligula ex, ultricies dignissim dui eget, suscipit elementum tellus. Aliquam erat volutpat. Mauris semper quam vitae faucibus accumsan. Duis laoreet auctor tellus, pulvinar elementum est pellentesque ornare. Morbi bibendum scelerisque luctus. Phasellus elementum dignissim sapien sed dignissim.";
async function CountChar (sentence) {
const count = await sentence.length;
const result = (count * 1000000000); // making a big calculation
console.log(result);
}
CountChar(line); // this will print at the last because its big calculation then other and this is the magic of async function.
// those will print first because and not hold exicution
console.log("I am first");
console.log("I am second");
console.log("I am third");