-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongpoll.js
40 lines (39 loc) · 997 Bytes
/
longpoll.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function longpoll_status(url, cb, id = null, seen = null) {
let opts = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: {},
};
let body = {};
if (id) {
body.id = id;
}
if (seen) {
body.seen = seen;
}
opts.body = JSON.stringify(body);
fetch(url, opts)
.then(function (response) {
// First check the response itself. Fetch the result as json or
// turn it into an an error if not ok.
if (response.ok) {
return response.json(); // this is promise, not the object
} else {
return response.text().then(t => Promise.reject(t))
}
})
.then(function (response_body) {
// schedule the next poll
setTimeout(longpoll_status, 1000, url, cb, response_body.id, response_body.seen);
// pass the current response down the line
return Promise.resolve(response_body);
})
.then(function (response_body) {
cb(response_body);
})
// .catch(function (err) {
// console.log("ERROR: " + JSON.stringify(err));
// })
}