In my project there is a function someFunction which use fetch api to read file from http server so its slow.
I want to repalce this someFunction by my mock_someFunction which will read file from filesystem. Reading from file system would be faster then http call.
// this use http call to fetch file, this is slow
function someFunction(fileName) {
let url = `https://${HOST}/${fileName}`
let e = fetch(url).then(function (response) {
// response is instance of Response
return response.arrayBuffer();
});
return e;
}
// this fetch file from file system, this would be fast
function mock_someFunction(fileName) {
// we already have this file on file system so lets not use fetch api
let e = localFetch(fileName).then(function (response) {
// response should be instance of Response
return response.arrayBuffer();
})
return e;
}
// implement this
function localFetch(fileName) {
// read file
var myBlob = new Blob();
var init = { "status" : 200 , "statusText" : "SuperSmashingGreat!" };
var myResponse = new Response(myBlob,init);
// return reposnce promise
}
What should be way to create mock_someFunction function to make it replaceable by someFunction?