by

Promise-Defer..... then-bindsTo   (100% working)

highly performant async transmission agent. functional units are expressed with a declarative syntax.    Just try and break this promise,
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/250852/Promise-Defer------then-bindsTo---(100--?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
// deferred execution!!! this.Promise = function () { return { status: 'pending', error: null, posCBKs: [], negCBKs: [], then: function (posCBK, negCBK) { var defer = new Deferred(); posCBK = (!posCBK)? function(d){d = (d || true); return d;}:posCBK; negCBK = (!negCBK)? function(d){d = (d || true); return d;}:negCBK; this.posCBKs.push({ func: posCBK, deferred: defer }); this.negCBKs.push({ func: negCBK, deferred: defer }); if (status === 'resolved') { this.execAction({ func: posCBK, deferred: defer }, this.data); } else if (status === 'rejected') { this.execAction({ func: negCBK, deferred: defer }, this.error); } return defer.promise; }, execAction: function (callbackData, result) { wdw.setTimeout(function () { var res = callbackData.func(result); if (res instanceof Promise) { callbackData.deferred.bind(res); } else { callbackData.deferred.resolve(res); } }, 0); } }; }; this.Deferred = function () { return { promise: new Promise(), resolve: function (data) { var pms = this.promise; pms.data = data; pms.status = 'resolved'; if (pms.posCBKs) { pms.posCBKs.forEach(function (dta) { pms.execAction(dta, data); }); } }, reject: function (err) { var pms = this.promise; pms.error = err; pms.status = 'rejected'; if (pms.negCBks) { pms.negCBKs.forEach(function (dta) { pms.execAction(dta, err); }); } }, bind: function (promise) { var slf = this; promise.then(function (dta) { slf.resolve(dta); }, function (err) { slf.reject(err); }); } }; };
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus