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