-3
I need a simple help with javascript modules, I am studying and I have many questions.
In my example, I charge Fetch.js,
which is a unique file that I use to make requests, GET, PUT, POST e DELETE
, and everything is going well so far.
So I have a file AppController.js
, where I get some data from the application, such as the name of the application, for example.
Appcontroller.js
import Fetch from './Fetch.js';
// GET
async function getApplication() {
return await Fetch.get('/application');
}
export default getApplication;
So I have another file Index.js
, where do I matter AppController.js
Index.js
import InfoApplication from './AppController.js';
let ObjectApplication = {};
const initApplication = () => {
InfoApplication();
}
initApplication();
Fetch js.
// Fetch.js
const _apiHost = 'http://banco_de_dados';
async function request(url, params, method = 'GET') {
const options = {
method,
headers: {
'Content-Type': 'application/json'
}
};
if (params) {
if (method === 'GET') {
url += '?' + objectToQueryString(params);
} else {
options.body = JSON.stringify(params);
}
}
const response = await fetch(_apiHost + url, options);
if (response.status !== 200) {
return generateErrorResponse('The server responded with an unexpected status.');
}
const result = await response.json();
return result;
}
function objectToQueryString(obj) {
return Object.keys(obj).map(key => key + '=' + obj[key]).join('&');
}
function generateErrorResponse(message) {
return {
status : 'error',
message
};
}
function get(url, params) {
return request(url, params);
}
function create(url, params) {
return request(url, params, 'POST');
}
function update(url, params) {
return request(url, params, 'PUT');
}
function remove(url, params) {
return request(url, params, 'DELETE');
}
export default {
get,
create,
update,
remove
};
And here begins my doubt:
Question:
I want to store the request return on a global object, so I can access the properties of that object anywhere, it’s possible?
For example:
ObjectApplication.name
if you are only using nodejs or some js framework?
– novic
I did not find the IIFE quoted question. I would not be trying to do something like this
(function () {InfoApplication()})()
, even if it means the same as onlyInfoApplication()
– Augusto Vasques
@Virgilionovic, I’m not using nodejs and I’m not using any
webpack
or framework. It’s native JS!– Wagner Fillio
It’s wrong here:
getApplication()
because of the inner part and lack so much that your question is bad, has no foundation, has no way to answer and did not say to what came ...– novic
@Augustovasques, it helps me about question 1, but I have the second question that for me is more complex to understand, I thought about
singletons
, but I can’t imagine how to use this module.– Wagner Fillio
do not ask two questions in one. It is unpleasant to answer and is contrary to the rules of the site.
– Augusto Vasques
Where are you doing it ... since you don’t use nodejs?
– novic
I am doing in pure js, only created the files in . js and loading an HTML file like this:
<script type="module" src"./index.js"></scritp>
– Wagner Fillio
and there is no error on the screen, because it should?
– novic
You’re not making any mistakes! I just want to know if it is possible to store the return of the request in an object and access this object anywhere in the application
– Wagner Fillio
Yes it is possible.
– Augusto Vasques
may not be wrong, but for example your
getApplication()
does nothing he just has aconst
with call on aFetch
that we do not know the implementation and if the request can be made withfetch
... I don’t know I think your code has several problems.– novic
is possible but not with that code...
– novic
@Virgilionovic, the
getApplication()
makes a request, see that at the beginning of the file, I also import another Fetch.js file, and in this Fetch, I have the get method– Wagner Fillio
That one
getApplication()
gives you a const with a promisse, but, this is not solved, IE, we do not know how it was implemented and we have no way to guess. or puts all the code or rephrases the question and it seems that your doubt is up to another.– novic
@Virgilionovic, I put all the code
– Wagner Fillio
your
getApplication()
needs only onereturn
thenasync function getApplication() {
 return await Fetch.get('/application');
}
– novic
And what difficulty did you find to store the request return in a global variable?
– user142154
@Virgilionovic, great, also worked with
return
, but I want this return to stay in this objectObjectApplication
– Wagner Fillio
@v.Saints my difficulty is how to store the return of the request in the objectApplication
– Wagner Fillio