1. class CustomPromise {
    2. state = "PENDING"
    3. value = undefined
    4. thenCallbacks = []
    5. errorCallbacks = []
    6. constructor(action) {
    7. action(this.resolver.bind(this), this.reject.bind(this))
    8. }
    9. resolver(value) {
    10. this.state = "RESOLVED"
    11. this.value = value
    12. this.thenCallbacks.forEach((callback) => {
    13. callback(this.value)
    14. })
    15. }
    16. reject(value) {
    17. this.state = "REJECTED"
    18. this.value = value
    19. this.errorCallbacks.forEach((callback) => {
    20. callback(this.value)
    21. })
    22. }
    23. then(callback) {
    24. this.thenCallbacks.push(callback)
    25. return this
    26. }
    27. catch (callback) {
    28. this.errorCallbacks.push(callback)
    29. return this
    30. }
    31. }
    32. let promise = new CustomPromise((resolver, reject) => {
    33. setTimeout(() => {
    34. const rand = Math.ceil(Math.random(1 * 1 + 6) * 6)
    35. if (rand > 2) {
    36. resolver("Success")
    37. } else {
    38. reject("Error")
    39. }
    40. }, 1000)
    41. })
    42. promise
    43. .then(function(response){
    44. console.log(response)
    45. })
    46. .catch(function(error){
    47. console.log(error)
    48. })