Add async primitives for http queries, failure handling and mapping non-async functions

This commit is contained in:
Tissevert 2019-02-13 08:26:08 +01:00
parent 52a7e883fa
commit 2f41708e7a
1 changed files with 32 additions and 0 deletions

View File

@ -2,6 +2,9 @@ function Async() {
return {
apply: apply,
bind: bind,
fail: fail,
http: http,
map: map,
parallel: parallel,
run: run,
sequence: sequence,
@ -35,6 +38,20 @@ function Async() {
};
}
function fail(message) {
return function(f) {
console.log(message);
}
}
function map(mapper) {
return function(x) {
return function(f) {
f(mapper(x));
};
};
}
function parallel() {
var threads = arguments;
var pending = threads.length;
@ -96,4 +113,19 @@ function Async() {
};
}
function http(query) {
return function(f) {
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function() {
f(this);
});
xhr.open(query.method, query.url);
if(query.method == 'POST') {
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
}
xhr.send(query.body);
};
}
}