1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| function Promise(fn){ var status = 'pending' function successNotify(){ status = 'resolved' toDoThen.apply(undefined, arguments) } function failNotify(){ status = 'rejected' toDoThen.apply(undefined, arguments) } function toDoThen(){ setTimeout(()=>{ if(status === 'resolved'){ for(let i =0; i< successArray.length;i ++) { successArray[i].apply(undefined, arguments) } }else if(status === 'rejected'){ for(let i =0; i< failArray.length;i ++) { failArray[i].apply(undefined, arguments) } } })
} var successArray = [] var failArray = [] fn.call(undefined, successNotify, failNotify) return { then: function(successFn, failFn){ successArray.push(successFn) failArray.push(failFn) return undefined } } }
|