javascript - promise with ajax doesn't return any value in jquery -
my script:
var testapp = (function($){ var data = [{ "layout": "getsample", "view": "conversations", "format": "json", }]; var data = 'default'; function ajaxcall(opt) { return new promise(function(resolve, reject) { jquery.ajax({ method: "post", url: localstorage.getitem("root")+"/index.php", "data": opt, error: function() { alert('error'); }, success: function(result) { console.debug(result); resolve(result); }//end success });//end ajax });//end promise } return { render: function(opt) { if(typeof opt === 'object') { var list = { data : [opt] } //here i'm passing list object's data used in ajaxcall function.that's reeason used call method. it's data passed page. ajaxcall.call (list, list.data).then(function(v) { console.log("v "+v); // nothing happens yet...expecting success object passed here }).catch(function(v) { //nothing yet }); } } };//end return })(jquery);
is correct way of using promise ajax?
ajaxcall.call (list, list.data).then(function(v) { console.log("v "+v); // doesn't return }).catch(function(v) { //nothing yet });
referred: how return response asynchronous call?
well, simple fix found.. //below line of code, moved above return new promise , worked
var opt = jquery.extend({}, data[0], opt[0]);
Comments
Post a Comment