From 71aaa607b9a46483ce23387ae691894923caea95 Mon Sep 17 00:00:00 2001 From: Laurent Drogou Date: Tue, 29 Mar 2022 17:07:13 +0200 Subject: [PATCH] feat: exercice creating and queuing Promises --- src/creating.mjs | 97 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 90 insertions(+), 7 deletions(-) diff --git a/src/creating.mjs b/src/creating.mjs index 22c76fe..2bfc804 100644 --- a/src/creating.mjs +++ b/src/creating.mjs @@ -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() { } \ No newline at end of file