From 332bc2d6bdcf66b5a77d39e3397c9304e1175d48 Mon Sep 17 00:00:00 2001 From: aminamos <26092352+aminamos@users.noreply.github.com> Date: Tue, 7 Apr 2020 08:52:28 -0700 Subject: [PATCH] return to master state --- index.js | 9 ++--- package.json | 6 ++-- src/consuming.mjs | 57 ----------------------------- src/creating.mjs | 92 +---------------------------------------------- src/iterating.mjs | 52 ++++----------------------- 5 files changed, 11 insertions(+), 205 deletions(-) diff --git a/index.js b/index.js index 2720996..0aea3b5 100644 --- a/index.js +++ b/index.js @@ -35,11 +35,6 @@ server.use("/service-worker.js", (req, res) => ); server.use(router); -// server.listen(3000, () => { -// console.log("JSON Server is running on port 3000"); -// }); -// allow for port 3001 -const port = process.env.PORT || 3000 -server.listen(port, () => { - console.log(`JSON Server is running on port ${port}`); +server.listen(3000, () => { + console.log("JSON Server is running on port 3000"); }); \ No newline at end of file diff --git a/package.json b/package.json index 5a63f32..b76b7cc 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,7 @@ "description": "Source code for the Pluralsight course JavaScript Promises and Async Programming", "main": "index.js", "scripts": { - "dev": "nodemon ./index.js", - "secondary": "cross-env PORT=3001 nodemon ./index.js" + "dev": "nodemon ./index.js" }, "repository": { "type": "git", @@ -18,11 +17,10 @@ }, "homepage": "https://github.com/taylonr/async-programming-promises#readme", "dependencies": { - "cross-env": "^7.0.2", "express": "^4.17.1", "json-server": "^0.15.1" }, "devDependencies": { "nodemon": "^2.0.2" } -} +} \ No newline at end of file diff --git a/src/consuming.mjs b/src/consuming.mjs index 4b372e8..8090053 100644 --- a/src/consuming.mjs +++ b/src/consuming.mjs @@ -1,73 +1,16 @@ import setText, {appendText, showWaiting, hideWaiting} from "./results.mjs"; export function get() { - axios.get("http://localhost:3000/orders/1") - .then(({data}) => { - setText(JSON.stringify(data)) - }) } export function getCatch() { - axios.get("http://localhost:3000/orders/123") - .then(({data}) => { - setText(JSON.stringify(data)) - }) - .catch(error => setText(error)) } export function chain() { - axios.get("http://localhost:3000/orders/1") - .then(({data}) => { - return axios.get(`http://localhost:3000/addresses/${data.shippingAddress}`) - }) - .then(({data}) => { - setText(`City: ${data.city}`) - }) } export function chainCatch() { - // axios.get("http://localhost:3000/orders/1") - // .then(({data}) => { - // axios.get(`http://localhost:3000/addresses/${data.shippingAddress}`) - - // throw new Error("Error") - // }) - // .catch(err => { - // setText(err); - - // throw new Error("Second error") - // }) - // .then(({data}) => { - // setText(`City: ${data.my.city}`) - // }) - // .catch(err => setText(err)) - - 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(error => setText(error)) } 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(error => setText(error)) - .finally(() => { - // setTimeout(() => { - // hideWaiting() - // }, 1500); - hideWaiting() - appendText(" done") - }) } \ No newline at end of file diff --git a/src/creating.mjs b/src/creating.mjs index dce5291..dbf51e2 100644 --- a/src/creating.mjs +++ b/src/creating.mjs @@ -1,112 +1,22 @@ -import setText, { appendText } from "./results.mjs"; +import setText 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("interval") - resolve(`Timeout ${++counter}`) - }, 1500); - }) - - wait - .then(text => setText(text)) - .finally(() => appendText(` -- Done ${counter}`)) } export function clearIntervalChain(){ - let counter = 0 - let interval - const wait = new Promise(resolve => { - interval = setInterval(() => { - console.log("interval") - resolve(`Timeout ${++counter}`) - }, 1500); - }) - - wait - .then(text => setText(text)) - .finally(() => clearInterval(interval)) } export function xhr(){ - let request = new Promise((resolve, reject) => { - let xhr = new XMLHttpRequest() - xhr.open("GET", "http://localhost:3000/users/7") - xhr.onload = () => { - if (xhr.status === 200) { - resolve(xhr.responseText) - } else { - reject(xhr.statusText) - } - } - xhr.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 addressTypes = axios.get("http://localhost:3000/addressTypes") - - // wait until all promises - // fulfilled or one is rejected - Promise.all([categories, statuses, userTypes, addressTypes]) - .then(([cat, stat, type, address]) => { - setText(""); - - appendText(JSON.stringify(cat.data)) - appendText(JSON.stringify(stat.data)) - appendText(JSON.stringify(type.data)) - appendTest(JSON.stringify(address.data)) - }) - .catch(error => setText(error)) } 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 addressTypes = axios.get("http://localhost:3000/addressTypes") - - // wait until all are - // fulfilled or rejected - Promise.allSettled([categories, statuses, userTypes, addressTypes]) - .then((values) => { - let results = values.map(v => { - if (v.status === 'fulfilled') { - return `Fulfilled: ${JSON.stringify(v.value.data[0])} ` - } - - return `Rejected: ${v.reason.message} ` - }) - - setText(results) - }) - .catch(error => setText(error)) } export function race(){ - let users = axios.get("http://localhost:3000/users") - let backup = axios.get("http://localhost:3001/backup") - - // return fastest promise - Promise.race([users, backup]) - .then(users => setText(JSON.stringify(users.data))) - .catch(error => setText(error)) } \ No newline at end of file diff --git a/src/iterating.mjs b/src/iterating.mjs index 826ffd1..849f352 100644 --- a/src/iterating.mjs +++ b/src/iterating.mjs @@ -1,56 +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(){ } -// even if orders finishes first, -// data won't show until orderStatus finishes -// then it'll move to orders -export async function concurrent(){ - const orderStatus = axios.get("http://localhost:3000/orderStatuses") - const orders = axios.get("http://localhost:3000/orders") - - setText("") - - const {data: statuses} = await orderStatus - const {data: allOrders} = await orders - - appendText(JSON.stringify(statuses)) - appendText(JSON.stringify(allOrders[0])) +export function concurrent(){ } -// run at same time -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(){ +} \ No newline at end of file