import {GameUtils} from './GameUtils'; export default class SPromise{ private _waitingList:((...arg) => void)[] = []; private _resolvedArguments:any[]; private _isResolved:boolean; constructor(){ this.reset(); } resolve(...args){ if(this._isResolved) { console.error('SPromise::resolve 已经被resolve过了'); return; } this._resolvedArguments = args; for(let fuc of this._waitingList) { fuc.apply(null, this._resolvedArguments); } this._waitingList.length = 0; this._isResolved = true; } then(fuc:(...arg) => void){ if(this._isResolved) { fuc.apply(null, this._resolvedArguments); } else { GameUtils.pushElemToAryIfNotIn(fuc, this._waitingList); } } reset(){ this._isResolved = false; } }