by

PROMISE!   X(ml)   2  J(son)!

makE  XML PROMISE to  be  JSON  DEFERRED....
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/440082/PROMISE!---X(ml)---2--J(son)!?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
var xp = (function(){ var 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) { setTimeout(function () { var res = callbackData.func(result); if (res instanceof Promise) { callbackData.deferred.bind(res); } else { callbackData.deferred.resolve(res); } }, 0); } }; }, 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); }); } }; }; // XML to JSON module var xj = function(xml) { var i=0,xJsn={},len={}; if (xml.nodeType == 1) { if (xml.attributes.length > 0) { len = xml.attributes.length; xJsn["@attributes"] = {}; for (;;++i<len) { var xAItm = xml.attributes.item(i); xJsn["@attributes"][xAItm.nodeName] = xAItm.nodeValue; } } } else if(xml.nodeType == 3) { xJsn = xml.nodeValue; } if (xml.childNodes) { i=0,len=xml.childNodes.length; for(;;++i<len) { var xnd = xml.childNodes.item(i), xndNm = xnd.nodeName; if (!xJsn[xndNm]) { xJsn[xndNm] = xmlToJson(xnd); } else { if (!xJsn[xndNm].push) { var orig=xJsn[xndNm]; xJsn[xndNm]=[]; xJsn[xndNm].push(orig); } xJsn[xndNm].push(X2J(xnd)); } } } return xJsn; }; return { x2j: function(xml, func) { var dfd = new Deferred(); try { var rezlt = xj(xml); dfd.resolve(rezlt); if(func) { func(rezlt); } } catch(er) { dfd.reject(er); } return dfd.promise; } }; }());
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus