2013-02-10 14 views
5

Ho visto un paio di pezzi di codice recentemente che assomigliano così:Aggiungere un metodo per poi una funzione per le richiamate

myFunc(args).then(function() { ... }); 

Trovo questa sintassi richiamata davvero elegante. La mia comprensione è che questo non fa parte di vanilla JS, e mi piacerebbe poterlo usare occasionalmente senza dipendere da particolari librerie, quindi sono interessato a come implementarlo da solo. Quindi, come funziona questo genere di cose e come lo implementeresti per una chiamata di funzione?

+0

Il metodo "then" tende a far parte del modello "promessa". jQuery lo implementa come parte dell'api "differita". – zzzzBov

+0

[douglas crockford tocca brevemente le promesse nel suo discorso "Monadi e gonadi"] (http://www.youtube.com/watch?v=dkZFtimgAcM&t=1920) – zzzzBov

+0

Leggi la proposta di promessa di CommonJS che include anche un elenco di librerie puoi usare: http://wiki.commonjs.org/wiki/Promises/A. –

risposta

4

Questo modello è chiamato "promesse". È implementato da jQuery e dojo, tra gli altri, e un approccio sarebbe quello di guardare il loro codice e vedere come lo implementano.

Il modello di implementazione generale sta creando una funzione che restituisce un oggetto che include una funzione (quindi) per passare una coppia di funzioni come richiami al metodo precedente che verrà quindi eseguito in caso di esito positivo o negativo. MSDN ha più le promesse in un blog post here

C'è un'implementazione minimale postato su GitHub qui: Promises GIST

function Promise() { 
    this._thens = []; 
} 

Promise.prototype = { 

    /* This is the "front end" API. */ 

    // then(onResolve, onReject): Code waiting for this promise uses the 
    // then() method to be notified when the promise is complete. There 
    // are two completion callbacks: onReject and onResolve. A more 
    // robust promise implementation will also have an onProgress handler. 
    then: function (onResolve, onReject) { 
     // capture calls to then() 
     this._thens.push({ resolve: onResolve, reject: onReject }); 
    }, 

    // Some promise implementations also have a cancel() front end API that 
    // calls all of the onReject() callbacks (aka a "cancelable promise"). 
    // cancel: function (reason) {}, 

    /* This is the "back end" API. */ 

    // resolve(resolvedValue): The resolve() method is called when a promise 
    // is resolved (duh). The resolved value (if any) is passed by the resolver 
    // to this method. All waiting onResolve callbacks are called 
    // and any future ones are, too, each being passed the resolved value. 
    resolve: function (val) { this._complete('resolve', val); }, 

    // reject(exception): The reject() method is called when a promise cannot 
    // be resolved. Typically, you'd pass an exception as the single parameter, 
    // but any other argument, including none at all, is acceptable. 
    // All waiting and all future onReject callbacks are called when reject() 
    // is called and are passed the exception parameter. 
    reject: function (ex) { this._complete('reject', ex); }, 

    // Some promises may have a progress handler. The back end API to signal a 
    // progress "event" has a single parameter. The contents of this parameter 
    // could be just about anything and is specific to your implementation. 
    // progress: function (data) {}, 

    /* "Private" methods. */ 

    _complete: function (which, arg) { 
     // switch over to sync then() 
     this.then = which === 'resolve' ? 
      function (resolve, reject) { resolve(arg); } : 
      function (resolve, reject) { reject(arg); }; 
     // disallow multiple calls to resolve or reject 
     this.resolve = this.reject = 
      function() { throw new Error('Promise already completed.'); }; 
     // complete all waiting (async) then()s 
     var aThen, i = 0; 
     while (aThen = this._thens[i++]) { aThen[which] && aThen[which](arg); } 
     delete this._thens; 
    } 

}; 

(Si noti che questo non è il mio codice ho guardato attraverso di essa e si guarda bene come punto di partenza. , ma tutto il merito va allo original author)

Problemi correlati