feat: exercice Iterating with Async Await

This commit is contained in:
Laurent Drogou
2022-03-30 19:51:04 +02:00
parent 71aaa607b9
commit 2b33f9e73f

View File

@@ -1,16 +1,53 @@
import setText, { appendText } from './results.mjs';
export function get(){
export async function get() {
const { data } = await axios.get("http://localhost:3000/orders/1");
setText(JSON.stringify(data));
}
export function getCatch(){
export async function getCatch() {
try {
const { data } = await axios.get("http://localhost:3000/orders/123");
setText(JSON.stringify(data));
} catch(error) {
setText(error);
}
}
export function chain(){
export async function chain() {
const {data} = await axios.get("http://localhost:3000/orders/1");
const {data: address} = await axios.get(`http://localhost:3000/addresses/${data.shippingAddress}`);
setText(`City: ${JSON.stringify(address.city)}`);
}
export function concurrent(){
export async function concurrent() {
const orderStatuses = axios.get("http://localhost:3000/orderStatuses");
const orders = axios.get("http://localhost:3000/orders");
const {data: statuses} = await orderStatuses;
const {data: order} = await orders;
appendText(JSON.stringify(statuses));
appendText(JSON.stringify(order[0]));
}
export function parallel(){
export async function parallel() {
setText("")
await Promise.all([
(async () => {
const { data } = await axios.get("http://localhost:3000/orderStatuses");
appendText(JSON.stringify(data));
}) (),
(async () => {
const { data } = await axios.get("http://localhost:3000/orders");
appendText(JSON.stringify(data));
}) ()
])
}