Compare commits

..

1 Commits

Author SHA1 Message Date
Laurent Drogou
488fe1772c feat: exercice 1 partiel 2022-03-25 23:06:07 +01:00
3 changed files with 13 additions and 159 deletions

View File

@@ -26,33 +26,7 @@ export function chain() {
}
export function chainCatch() {
axios.get("http://localhost:3000/orders/1")
.then(({ data }) => {
axios.get(`http://localhost:3000/addresses/${data.shippingAddress}`);
})
.then(({data}) => {
setText(`City: ${data.city}`);
})
.catch(err => setText(err));
}
export function final() {
showWaiting();
axios.get("http://localhost:3000/orders/1")
.then(({ data }) => {
return axios.get(`http://localhost:3000/addresses/${data.shippingAddress}`);
})
.then(({data}) => {
setText(`City: ${data.city}`);
})
.catch(err => setText(err))
.finally(() => {
setTimeout(() => {
hideWaiting();
}, 1500);
appendText(" -- Completely Done ");
});
}

View File

@@ -1,104 +1,21 @@
import setText, { appendText } from "./results.mjs";
export function timeout(){
const wait = new Promise((resolve) => {
setTimeout(() => {
resolve("Timeout !");
}, 1500)
});
wait.then(text => setText(text))
}
export function interval(){
let counter = 0;
const wait = new Promise((resolve) => {
setInterval(() => {
console.log(`Counter ${++counter}`);
resolve(`Counter ${counter}`);
}, 1500)
});
wait
.then(text => setText(text))
.finally(() => appendText(`-- DONE COUNTER ${counter}`));
}
export function clearIntervalChain(){
let counter = 0;
let interval;
const wait = new Promise((resolve) => {
interval = setInterval(() => {
console.log(`Counter ${++counter}`);
resolve(`Counter ${counter}`);
}, 1500)
});
wait
.then(text => setText(text))
.finally(() => clearInterval(interval));
}
export function xhr(){
let request = new Promise((resolve, reject) => {
let oReq = new XMLHttpRequest();
oReq.open("GET", "http://localhost:3000/users/7");
oReq.onload = () => {
if (oReq.status === 200) {
resolve(oReq.responseText);
} else {
reject(oReq.statusText + " " + oReq.status);
}
}
oReq.onerror = () => reject("Request Failed !")
oReq.send();
});
request.then(result => setText(result))
.catch(reason => setText(reason));
}
export function allPromises(){
let categories = axios.get("http://localhost:3000/itemCategories");
let statuses = axios.get("http://localhost:3000/orderStatuses");
let userTypes = axios.get("http://localhost:3000/userTypes");
let addressType = axios.get("http://localhost:3000/addressTypes");
Promise.all([categories, statuses, userTypes, addressType])
.then(([cat, stat, type, addr]) => {
setText("");
appendText(JSON.stringify(cat.data));
appendText(JSON.stringify(stat.data));
appendText(JSON.stringify(type.data));
appendText(JSON.appendText(addr.data));
})
.catch(reasons => setText(reasons));
}
export function allSettled(){
let categories = axios.get("http://localhost:3000/itemCategories");
let statuses = axios.get("http://localhost:3000/orderStatuses");
let userTypes = axios.get("http://localhost:3000/userTypes");
let addressType = axios.get("http://localhost:3000/addressTypes");
Promise.allSettled([categories, statuses, userTypes, addressType])
.then((values) => {
let results = values.map(v => {
if (v.status === 'fulfilled'){
return `FULLFILLED ${JSON.stringify(v.value.data[0])} `;
}
return `REJECTED : ${v.reason.message} `;
});
setText(results)
})
.catch(toto => setText(toto));
}
export function race(){

View File

@@ -1,53 +1,16 @@
import setText , {appendText} from './results.mjs';
export async function get() {
const { data } = await axios.get("http://localhost:3000/orders/1");
setText(JSON.stringify(data));
export function get(){
}
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 getCatch(){
}
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 chain(){
}
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 concurrent(){
}
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));
}) ()
])
export function parallel(){
}