shop.js 720 B

123456789101112131415161718192021222324252627282930313233
  1. /**
  2. * Mocking client-server processing
  3. */
  4. const _products = [
  5. { 'id': 1, 'title': 'iPad 4 Mini', 'price': 500.01, 'inventory': 2 },
  6. { 'id': 2, 'title': 'H&M T-Shirt White', 'price': 10.99, 'inventory': 10 },
  7. { 'id': 3, 'title': 'Charli XCX - Sucker CD', 'price': 19.99, 'inventory': 5 }
  8. ]
  9. export default {
  10. async getProducts () {
  11. await wait(100)
  12. return _products
  13. },
  14. async buyProducts (products) {
  15. await wait(100)
  16. if (
  17. // simulate random checkout failure.
  18. (Math.random() > 0.5 || navigator.webdriver)
  19. ) {
  20. return
  21. } else {
  22. throw new Error('Checkout error')
  23. }
  24. }
  25. }
  26. function wait (ms) {
  27. return new Promise(resolve => {
  28. setTimeout(resolve, ms)
  29. })
  30. }