Skip to content

Commit 85053a4

Browse files
committed
Use promises for amcp command
1 parent 346d8b6 commit 85053a4

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

core/core.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,32 @@ function next() {}
6363
function update(data) {}
6464

6565

66-
var amcp_bridge_url = "http://127.0.0.1:9731/amcp";
66+
var amcp_bridge_url = "http://127.0.0.1:9731/amcp"
67+
var amcp_bridge_timeout = 5000
6768

6869
function amcp(command){
69-
log("SENDING " + command)
70-
var xhr = new XMLHttpRequest()
71-
xhr.open('POST', amcp_bridge_url)
72-
xhr.responseType = "text"
73-
xhr.send(command)
70+
return new Promise(function(resolve, reject){
7471

75-
xhr.onload = function() {
76-
};
72+
log("AMCP TX: " + command)
7773

78-
xhr.onerror = function() {
79-
log("Status:" + xhr.status + " : " + xhr.statusText)
80-
log(xhr.responseText)
81-
}
74+
var xhr = new XMLHttpRequest()
75+
xhr.timeout = amcp_bridge_timeout
76+
xhr.open('POST', amcp_bridge_url)
77+
xhr.responseType = "text"
78+
xhr.send(command)
79+
80+
xhr.onload = function() {
81+
log("AMCP RX: " + xhr.status + " " + command)
82+
if (xhr.status < 400)
83+
resolve(xhr.status, xhr.responseText)
84+
else
85+
reject(xhr.status, xhr.responseText)
86+
};
87+
88+
xhr.onerror = function() {
89+
log("AMCP RX ERR:", xhr.status)
90+
reject(xhr.status, xhr.responseTextx)
91+
}
92+
93+
}) // return new Promise
8294
}

0 commit comments

Comments
 (0)