Compare commits

..

3 Commits

Author SHA1 Message Date
Laurent Drogou
2b33f9e73f feat: exercice Iterating with Async Await 2022-03-30 19:51:04 +02:00
Laurent Drogou
71aaa607b9 feat: exercice creating and queuing Promises 2022-03-29 17:07:13 +02:00
Laurent Drogou
3cb77be647 feat: ex Consuming pormises 2022-03-29 15:07:08 +02:00
3 changed files with 159 additions and 13 deletions

View File

@@ -26,7 +26,33 @@ 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,22 +1,105 @@
import setText, { appendText } from "./results.mjs";
export function timeout(){
export function timeout() {
const wait = new Promise((resolve) => {
setTimeout(() => {
resolve("Timeout !");
}, 1500)
});
wait.then(text => setText(text))
}
export function interval(){
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(){
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(){
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(){
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(){
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(){
export function race() {
}

View File

@@ -1,16 +1,53 @@
import setText , {appendText} from './results.mjs';
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));
}) ()
])
}