I´m currently working on a small microservice application, which will in it´s simplest form just receive a Request from a Formular and and based on containing information perform several other Server-request to different API´s using the Fetch API
.
For now I´m just about to create a prototype that contains the Application structure and the promise-chain structure which I´d prefer to use.
I`ve created a prototype which is showing what I´m doing here. My Question is now if this is the right use of promises in which errors i would probably run in a production mode.
I´m currently feeling like I would do it wrong, therefore I´m asking for an advice here.
This is my Controller class which receives the Request:
'use strict';
var indexService = require('../services/index');
class IndexController {
constructor(router) {
this.router = router;
this.registerRoutes();
}
registerRoutes() {
this.router.get('/', this.index.bind(this));
}
index(req, res){
indexService.test()
.then(data => res.json(data))
.then(test => console.log("Maybe have to do other things after repsonding to client"))
.catch(err => console.log(err));
}
}
module.exports = IndexController;
And this is my Service Class for the business logic:
'use strict';
const fetch = require("node-fetch");
const HttpsProxyAgent = require('https-proxy-agent');
class IndexService {
constructor() {
}
test(){
var _this = this;
return new Promise(function(resolve, reject) {
console.log("INSIDE PROMIES");
_this.getOne()
.then(function(data){
resolve(data);
})
.catch(function(error){
console.log("Not connection to endpoint possible 1");
reject(error);
});
})
.then(function(data){
//based on data I call two or three
console.log("ALSO REACHED THIS ONE");
if("a"){
_this.getTwo()
.then(function(data){
return data;
})
.catch(function(error){
console.log("Not connection to endpoint possible 2");
return error;
});
}else{
_this.getThree()
.then(function(data){
return data;
})
.catch(function(error){
console.log("Not connection to endpoint possible 2");
return error;
});
}
return data;
}).then(function(data){
console.log(data);
return data;
})
.catch(function(err){
console.log("ERROR REACHED");
console.log(err);
throw "error";
});
}
async getOne(){
let response = await fetch('https://jsonplaceholder.typicode.com/todos/1', {
agent: new HttpsProxyAgent("http://xxx:80"),
});
if(response.ok){
console.log("Response OK");
// console.log(response);
let data = await response.json();
return data;
}else{
console.log("Response NOT OK");
//console.log(response);
return null;
}
}
async getTwo(){
let response = await fetch('https://jsonplaceholder.typicode.com/todos/1', {
agent: new HttpsProxyAgent("http:/xxxx:80"),
});
if(response.ok){
console.log("Response OK");
// console.log(response);
let data = await response.json();
return data;
}else{
console.log("Response NOT OK");
// console.log(response);
return null;
}
}
async getThree(){
let response = await fetch('https://jsonplaceholder.typicode.com/todos/1', {
agent: new HttpsProxyAgent("http://xxx:80"),
});
if(response.ok){
console.log("Response OK");
// console.log(response);
let data = await response.json();
return data;
}else{
console.log("Response NOT OK");
// console.log(response);
return null;
}
}
}
module.exports = new IndexService();
To make my question more clear:
Am I using the promises correct here? Should I better do everything in the first then and just chain the async class to use
resolve or reject
on finish?What could be a problem with the current design when it gets more complex, because i feel like it is not really readable currently?
Should I use
new Promise(function(resolve, reject)
instead ofasync function
?
I´m pretty new to this topic and would love to learn from your knowledge!
Thanks.