XVoxel-ASync/lib/webfs.js

35 lines
721 B
JavaScript
Raw Normal View History

/* Functions */
get = function (url, mode) {
return fetch(url).then(function (response) {
if (response.status == 200) {
switch (mode) {
case "text":
return response.text();
break;
case "arraybuffer":
return response.arrayBuffer();
break;
default:
}
} else {
reject(new Error("Status code is not 200. "));
}
}).catch(function (err) {
return err;
});
};
module.exports = {
"get": function (path, mode, cb) {
var href = window.location.href;
var address = path.startsWith("./") ? href.substring(0, href.lastIndexOf("/")) + path.substring(1) : path;
get(address, mode).then(function (data) {
cb(null, data);
}).catch(function (err) {
cb(err);
});
}
};