2013-02-10 18 views
5

के लिए एक समारोह के लिए एक तो विधि जोड़ें मैं हाल ही में कोड की कुछ बिट्स कि इतने की तरह लग रहे देखा है:कॉलबैक

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

मैं इस कॉलबैक वाक्य रचना वास्तव में सुरुचिपूर्ण पाते हैं। मेरी समझ यह है कि यह वेनिला जेएस का हिस्सा नहीं है, और मैं इसे विशेष पुस्तकालयों के आधार पर अवसर पर इसका उपयोग करने में सक्षम होना चाहता हूं, इसलिए मुझे इस बात को ध्यान में रखना है कि इसे कैसे कार्यान्वित किया जाए। तो, इस तरह की चीज कैसे काम करती है, और आप फ़ंक्शन कॉल के लिए इसे कैसे कार्यान्वित करेंगे?

+0

'तब' विधि "वादा" पैटर्न का हिस्सा बनती है। jQuery इसे "स्थगित" एपीआई के हिस्से के रूप में लागू करता है। – zzzzBov

+0

[डगलस क्रॉकफोर्ड अपने "मोनाड्स और गोनाड्स" टॉक में वादे पर संक्षेप में छूता है] (http://www.youtube.com/watch?v=dkZFtimgAcM&t=1920) – zzzzBov

+0

कॉमनजेएस वादे प्रस्ताव के बारे में पढ़ें जिसमें पुस्तकालयों की एक सूची भी शामिल है आप इसका उपयोग कर सकते हैं: http://wiki.commonjs.org/wiki/Promises/A। –

उत्तर

4

इस पैटर्न को "वादे" कहा जाता है। यह jQuery और डोजो द्वारा दूसरों के बीच कार्यान्वित किया गया है, और एक दृष्टिकोण उनके कोड को देखना होगा और देखें कि वे इसे कैसे कार्यान्वित करते हैं।

सामान्य कार्यान्वयन पैटर्न एक ऐसा फ़ंक्शन तैयार कर रहा है जो किसी ऑब्जेक्ट को लौटाता है जिसमें फ़ंक्शन (फिर) को एक विधि को कॉलबैक के रूप में पिछली विधि में पास करने के लिए पास किया जाता है जो तब सफलता या विफलता पर चलाया जाएगा। MSDN एक blog post here

में वादों के बारे में अधिक है वहाँ एक minimalist कार्यान्वयन 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 पर जाते हैं)