From 2f41708e7a73548fa26bb40967718b3b75e073c1 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Wed, 13 Feb 2019 08:26:08 +0100 Subject: [PATCH] Add async primitives for http queries, failure handling and mapping non-async functions --- async.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/async.js b/async.js index 996771f..747a9be 100644 --- a/async.js +++ b/async.js @@ -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); + + }; + } + }