return to master state

This commit is contained in:
aminamos
2020-04-07 08:52:28 -07:00
parent 93b3c2ac48
commit 332bc2d6bd
5 changed files with 11 additions and 205 deletions

View File

@@ -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")
})
}

View File

@@ -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))
}

View File

@@ -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(){
}