1
|
/**
|
2
|
* part of Hachette
|
3
|
* Wrapping XMLHttpRequest into a Promise.
|
4
|
*
|
5
|
* Copyright (C) 2021 Wojtek Kosior
|
6
|
* Redistribution terms are gathered in the `copyright' file.
|
7
|
*/
|
8
|
|
9
|
function ajax_callback()
|
10
|
{
|
11
|
if (this.readyState == 4)
|
12
|
this.resolve_callback(this);
|
13
|
}
|
14
|
|
15
|
function initiate_ajax_request(resolve, reject, method, url)
|
16
|
{
|
17
|
const xhttp = new XMLHttpRequest();
|
18
|
xhttp.resolve_callback = resolve;
|
19
|
xhttp.onreadystatechange = ajax_callback;
|
20
|
xhttp.open(method, url, true);
|
21
|
try {
|
22
|
xhttp.send();
|
23
|
} catch(e) {
|
24
|
console.log(e);
|
25
|
setTimeout(reject, 0);
|
26
|
}
|
27
|
}
|
28
|
|
29
|
function make_ajax_request(method, url)
|
30
|
{
|
31
|
return new Promise((resolve, reject) =>
|
32
|
initiate_ajax_request(resolve, reject, method, url));
|
33
|
}
|
34
|
|
35
|
/*
|
36
|
* EXPORTS_START
|
37
|
* EXPORT make_ajax_request
|
38
|
* EXPORTS_END
|
39
|
*/
|