feat: exercice creating and queuing Promises
This commit is contained in:
@@ -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() {
|
||||
}
|
||||
Reference in New Issue
Block a user