return to master state
This commit is contained in:
9
index.js
9
index.js
@@ -35,11 +35,6 @@ server.use("/service-worker.js", (req, res) =>
|
|||||||
);
|
);
|
||||||
|
|
||||||
server.use(router);
|
server.use(router);
|
||||||
// server.listen(3000, () => {
|
server.listen(3000, () => {
|
||||||
// console.log("JSON Server is running on port 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}`);
|
|
||||||
});
|
});
|
||||||
@@ -4,8 +4,7 @@
|
|||||||
"description": "Source code for the Pluralsight course JavaScript Promises and Async Programming",
|
"description": "Source code for the Pluralsight course JavaScript Promises and Async Programming",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "nodemon ./index.js",
|
"dev": "nodemon ./index.js"
|
||||||
"secondary": "cross-env PORT=3001 nodemon ./index.js"
|
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -18,11 +17,10 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/taylonr/async-programming-promises#readme",
|
"homepage": "https://github.com/taylonr/async-programming-promises#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cross-env": "^7.0.2",
|
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"json-server": "^0.15.1"
|
"json-server": "^0.15.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"nodemon": "^2.0.2"
|
"nodemon": "^2.0.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,73 +1,16 @@
|
|||||||
import setText, {appendText, showWaiting, hideWaiting} from "./results.mjs";
|
import setText, {appendText, showWaiting, hideWaiting} from "./results.mjs";
|
||||||
|
|
||||||
export function get() {
|
export function get() {
|
||||||
axios.get("http://localhost:3000/orders/1")
|
|
||||||
.then(({data}) => {
|
|
||||||
setText(JSON.stringify(data))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCatch() {
|
export function getCatch() {
|
||||||
axios.get("http://localhost:3000/orders/123")
|
|
||||||
.then(({data}) => {
|
|
||||||
setText(JSON.stringify(data))
|
|
||||||
})
|
|
||||||
.catch(error => setText(error))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function chain() {
|
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() {
|
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() {
|
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")
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
@@ -1,112 +1,22 @@
|
|||||||
import setText, { appendText } from "./results.mjs";
|
import setText 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("interval")
|
|
||||||
resolve(`Timeout ${++counter}`)
|
|
||||||
}, 1500);
|
|
||||||
})
|
|
||||||
|
|
||||||
wait
|
|
||||||
.then(text => setText(text))
|
|
||||||
.finally(() => appendText(` -- Done ${counter}`))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function clearIntervalChain(){
|
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(){
|
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(){
|
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(){
|
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(){
|
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))
|
|
||||||
}
|
}
|
||||||
@@ -1,56 +1,16 @@
|
|||||||
import setText , {appendText} from './results.mjs';
|
import setText , {appendText} from './results.mjs';
|
||||||
|
|
||||||
export async function get(){
|
export function get(){
|
||||||
const {data} = await axios.get("http://localhost:3000/orders/1")
|
|
||||||
setText(JSON.stringify(data))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getCatch(){
|
export function getCatch(){
|
||||||
try {
|
|
||||||
const {data} = await axios.get("http://localhost:3000/orders/123")
|
|
||||||
setText(JSON.stringify(data))
|
|
||||||
} catch (error) {
|
|
||||||
setText(error)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function chain(){
|
export 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)}`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// even if orders finishes first,
|
export function concurrent(){
|
||||||
// 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]))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// run at same time
|
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))
|
|
||||||
})()
|
|
||||||
])
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user