1. //resolve和reject
    2. function Promise(fn) {
    3. this.status = 'pending'
    4. this.value = ''
    5. const _this = this
    6. try {
    7. fn(resolve, reject)
    8. } catch (e) {
    9. reject(e)
    10. }
    11. function resolve(value) {
    12. _this.value = value
    13. _this.status = 'resloved'
    14. }
    15. function reject(value) {
    16. _this.value = value
    17. _this.status = 'rejected'
    18. }
    19. }
    20. //then方法实现
    21. Promise.prototype.then = function (onReslved, onRejected) {
    22. if (this.status === 'resolved') {
    23. onReslved(this.value)
    24. }
    25. if (self.status === 'rejected') {
    26. onRejected(this.reason);
    27. }
    28. }