2013-02-10 6 views
5

する機能にそのメソッドを追加します。私はそうのように見える最近のコードのいくつかのビットを見てきました。私はこれがvanilla JSの一部ではないことを理解しています。特定のライブラリに依存することなく機会に利用できるようにしたいと考えています。では、このようなことはどのように機能し、関数呼び出しのためにどのように実装しますか?コールバック

+0

「then」メソッドは、「promise」パターンの一部になる傾向があります。 jQueryはそれを「遅延」されたAPIの一部として実装します。 – zzzzBov

+0

[ダグラス・クロフォードは彼の "モナドとゴナッド"の話で約束を簡単に述べる](http://www.youtube.com/watch?v=dkZFtimgAcM&t=1920) – zzzzBov

+0

CommonJS約束の提案について読むあなたはhttp://wiki.commonjs.org/wiki/Promises/Aを使うことができます。 –

答えて

4

このパターンを「約束」といいます。 jQueryとDojoによって実装されています。そのアプローチの1つは、コードを見て、実装方法を確認する方法です。

一般的な実装パターンでは、関数を組み込んだオブジェクトを返す関数を作成しています。この関数は、関数のペアをコールバックとして前のメソッドに渡し、成功または失敗のいずれかで実行されます。 MSDNは、ミニマリストの実装がありますblog post here

での約束についての詳細ここにGitHubの上で掲載している: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; 
    } 

}; 

を(これは私のコードではないことに注意してください、私はそれを見て、それが出発点として、よさそうです。すべてのクレジットはoriginal authorに行きます)

関連する問題