xhr helpers: wrap in promises
This commit is contained in:
parent
36f3299ddf
commit
7cfd04ffb4
26
js/common.js
26
js/common.js
|
@ -18,23 +18,35 @@ function exception_error(e, e_compat, filename, lineno, colno) {
|
|||
|
||||
function xhrPost(url, params, complete) {
|
||||
console.log("xhrPost:", params);
|
||||
return new Ajax.Request(url, {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
new Ajax.Request(url, {
|
||||
parameters: params,
|
||||
onComplete: complete
|
||||
onComplete: function(reply) {
|
||||
if (complete != undefined) complete(reply);
|
||||
|
||||
resolve(reply);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function xhrJson(url, params, complete) {
|
||||
return xhrPost(url, params, (reply) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
return xhrPost(url, params).then((reply) => {
|
||||
let obj = null;
|
||||
|
||||
try {
|
||||
const obj = JSON.parse(reply.responseText);
|
||||
complete(obj);
|
||||
obj = JSON.parse(reply.responseText);
|
||||
} catch (e) {
|
||||
console.error("xhrJson", e, reply);
|
||||
complete(null);
|
||||
}
|
||||
|
||||
})
|
||||
if (complete != undefined) complete(obj);
|
||||
|
||||
resolve(obj);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/* add method to remove element from array */
|
||||
|
|
Loading…
Reference in New Issue