Guest User

Untitled

a guest
Feb 24th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. // Creates a new promise that automatically resolves after some timeout:
  2. Promise.delay = function (time) {
  3. return new Promise((resolve, reject) => {
  4. setTimeout(resolve, time)
  5. })
  6. }
  7.  
  8. // Throttle this promise to resolve no faster than the specified time:
  9. Promise.prototype.takeAtLeast = function (time) {
  10. return new Promise((resolve, reject) => {
  11. Promise.all([this, Promise.delay(time)]).then(([result]) => {
  12. resolve(result)
  13. }, reject)
  14. })
  15. }
  16.  
  17. // Make sure this doesn't resolve for at least 300ms, useful for things like
  18. // keeping a loading spinner on screen just long enough to not look janky:
  19. axios.post(`/published-products`, payload)
  20. .takeAtLeast(300)
  21. .then(response => {
  22. this.loading = false
  23. // ...
  24. })
  25. .catch(response => {
  26. this.loading = false
  27. // ...
  28. })
Add Comment
Please, Sign In to add comment