How to ensure that one function completes before other function - node.js

I am fairly new to Node.js, and what I am trying to achieve is to have two separate functions. One for Auth and one for sending data (So that I don't run into rate login limits if I were to simply use a callback after conn.login finishes). I tried to set this up in node like this:
var _request = {
url: '/services/data/v45.0/actions/custom/flow/Test1',
method: 'POST',
body: JSON.stringify({
"inputs": [{}]
}),
headers: {
"Content-Type": "application/json"
}
};
var conn = new jsforce.Connection({
clientId: process.env.cliendId,
clientSecret: process.env.clientSecret,
version: "45.0"
});
function sfdcAuth() {
conn.login(process.env.sfdcUser, process.env.sfdcUserPass, (err, userInfo) => {
if (err) {
console.log(err)
}
conn = conn;
console.log("Done")
});
}
function sfdcQuery() {
conn.request(_request, function(err, resp) {
console.log(resp);
console.log(err)
});
}
sfdcAuth()
sfdcQuery()
But because js is asynchronous it runs the second function without waiting for the first function to finish.

The simplest way is to pass your second function as a callback to your first function, which it can call when it’s done:
function sfdcAuth(callback) {
conn.login(process.env.sfdcUser, process.env.sfdcUserPass, (err, userInfo) => {
if (err) {
console.log(err);
}
// Invoke callback when done
callback();
});
}
function sfdcQuery() {
conn.request(_request, function(err, resp) {
console.log(resp);
console.log(err);
});
}
// Pass second function as callback to the first
sfdcAuth(sfdcQuery);
You could also make use of promises:
function sfdcAuth(callback) {
return new Promise((resolve, reject) => {
conn.login(process.env.sfdcUser, process.env.sfdcUserPass, (err, userInfo) => {
if (err) {
reject(err);
}
resolve(userInfo);
});
});
}
function sfdcQuery() {
return new Promise((resolve, reject) => {
conn.request(_request, function(err, resp) {
if (err) {
reject(err);
}
resolve(resp);
});
});
}
// Wait for promise to resolve before invoking second function
sfdcAuth()
.then(result => {
// Do something with result
return sfdcQuery();
})
.then(result => {
// You can continue the chain with
// the result from "sfdcQuery" if you want
})
.catch(err => {
// Handle error
});

Related

Using SQL Server, nodejs and bycrypt, request ends before function is recieved

I'm fairly new to nodejs and have stumbled into a problem with my code.
The documentation for SQL Server and a guide I found on Youtube both handle their code this way, but after starting to use bycrypt I've noticed my function ends after the request is complete although I'm using .then().
Anyways, here's my code so far:
router.post('/login', (req, res) => {
getLoginDetails(req.body.username, req.body.password).then(result => {
console.log(result);
res.json(result);
})
});
async function getLoginDetails(username, password) {
await pool1Connect;
try {
const request = pool1.request();
request.input('username', sql.NVarChar, username);
request.query('SELECT * FROM users WHERE username = #username', (err, result) => {
if (err) {
return ({err: err})
}
if (result.recordset.length > 0) {
bcrypt.compare(password, result.recordset[0].user_password, (err, response) => {
if (response) {
console.log(result.recordset);
return(result.recordset);
} else {
return({message: "Wrong password or username!"})
}
})
return(result)
} else {
return({message: "user not found!"})
}
})
} catch (err) {
return err;
}
}
I tried logging both the request and the return value from the function getLoginDetails and the request log came faster, so I assume it's not waiting for the program to actually finish and I can't figure out why...
Sorry if that's obvious, but I'd love to get some help here!
EDIT:
router.post('/login', async (req, res) => {
// res.send(getLoginDetails(req.body.username, req.body.password))
await pool1Connect
try {
const request = pool1.request();
request.input('username', sql.NVarChar, req.body.username);
request.query('SELECT * FROM users WHERE username = #username', (err, result) => {
console.log(result);
bcrypt.compare(req.body.password, result.recordset[0].user_password, (err, response) => {
if (response) {
res.send(result);
} else {
res.send('wrong password')
}
})
//res.send(result)
})
} catch (err) {
res.send(err);
}
});
This code works, but when I tried to encapsulate it in a function it still didn't work.
#Anatoly mentioned .query not finishing in time which makes sense, but I thought mssql .query is an async function?
Your problem arises from an wrong assumption that callbacks and promises are alike, but on the contrary callbacks don't "respect" promise/async constructs
When the program hits the bottom of getLoginDetails the progrm execution has already split into 2 branches one branch returned you the (empty) result whereas the other one still busy with crypto operations.
Though it is true that an async function always returns a promise but that doesn't cover any future callbacks that might execute inside it. As soon as node reaches the end of function or any return statement the async function's promise get resolved(therefore future callbacks are meaningless), what you can do instead is handroll your own promise which encampasses the callbacks as well
router.post('/login', (req, res) => {
getLoginDetails(req.body.username, req.body.password))
.then((result)=>{
res.send(result);
})
.catch((err)=>{
res.send(err);
})
});
async function getLoginDetails(username, password) {
await pool1Connect
return new Promise( (resolve,reject) => {
try {
const request = pool1.request();
request.input('username', sql.NVarChar, username);
request.query('SELECT * FROM users WHERE username = #username', (err, result) => {
console.log(result);
bcrypt.compare(password, result.recordset[0].user_password, (err, response) => {
if (response) {
resolve(result);
} else {
resolve('wrong password')
}
})
})
} catch (err) {
reject(err);
}
});
}
You didn't return any result to getLoginDetails. Either you use async versions of request.query and bcrypt.compare (if any) or wrap request.query to new Promise((resolve, reject) like this:
const asyncResult = new Promise((resolve, reject) => {
request.query('SELECT ...
...
if (err) {
resolve({err: err}) // replace all return statements with resolve calls
}
...
})
const queryResult = await asyncResult;

Architecture in Node JS wirh Controller, Service and Repository

In order to keep clean architecture of my node.js I have controllers, services and repositories.
Data flow: controller -> service -> repository -> service -> controller.
In this flow (in simple user story) repository returns data to service and service to controller. But repository should process requests to external storage.
Right now I have the callback difficulty, how can I implement a nested callback between controller and repository?
My Controller:
exports.clientes_get = async function (req, res) {
Cliente.find(function(err,params) {
if (err)
res.send(err);
res.json(params);
});
}
My Service:
ClienteGet(req) {
var response;
repo.get(req.params.clienteId, (err, res) => {
response = res;
//console.log(response); -> have data
});
//console.log(response); -> doesnt have data
return response;
};
My Repository:
get(clienteId, data) {
mongoose.model('Cliente').findById(clienteId, data);
};
How can i do this?
Using Callbacks
If you insist on using callbacks:
Controller:
// notice that the `async` here was not necessary
exports.clientes_get = function (req, res) {
// the `function(err, params) {...}` here is your callback
ClienteGet(req, function(err, params) {
if (err) {
return res.send(err);
}
res.json(params);
});
}
Service:
ClienteGet(req, callback) {
// simply pass the callback argument directly to the `repo.get` call
repo.get(req.params.clienteId, callback);
};
Repository:
get(clienteId, callback) {
// again, just pass the callback here
mongoose.model('Cliente').findById(clienteId, callback);
};
Using Promises
I personally recommend wrapping you code in
Promises,
because they are more flexible than callbacks, and modern JavaScript uses
Promises heavily. It's pretty much the new standard... In fact, the async /
await syntax is leveraging Promises under the hood.
Here's a rough sketch of how you could use Promises:
Controller:
exports.clientes_get = function (req, res) {
return ClienteGet(req)
.then((data) => {
res.json(data);
})
.catch((err) => {
res.send(err);
});
}
Service:
ClienteGet(req) {
return new Promise((resolve, reject) => {
repo.get(req.params.clienteId, (err, res) => {
if (err) {
return reject(err);
}
resolve(res);
//console.log(response); -> have data
});
});
};
Repository:
get(clienteId) {
return new Promise((resolve, reject) => {
mongoose.model('Cliente').findById(clienteId, function(err, document) {
if (err) {
return reject(err);
}
resolve(document);
});
});
};

Making second api call after first api call returns data

I have two seperate functions that make API calls to different endpoints for return JSON Data using BlueBirdPromise.
const searchVenues = (type) => {
logger.debug('getVenues : type = ' + type);
const config = {
url: urlAPIServer + '/venue/available',
qs: {
type,
},
headers: {
'x-api-key': dataApiKey
}
};
return new BluebirdPromise((resolve, reject) => {
request.get(config, (err, response, body) => {
if (err) {
console.error(err);
reject(err);
} else {
resolve(JSON.parse(body));
}
});
});
};
const getVenuesWithCuisine = () => {
logger.debug('getVenuesWithCuisine');
const config = {
url: urlAPIServer + '/venue/viewvenuewithcuisine',
headers: {
'x-api-key': dataApiKey
}
};
return new BluebirdPromise((resolve, reject) => {
request.get(config, (err, response, body) => {
if (err) {
console.error(err);
reject(err);
} else {
resolve(JSON.parse(body));
}
});
});
};
Invoking the funtions seperately to get the data from the API and bind it to variable.
searchVenues(venueType).then((venues) => {
checkContextTimeout(context);
conversationContext.venueType = venueType;
conversationContext.venues = venues;
context.skill = conversationContext;
});
getVenuesWithCuisine().then((venueswithcuisines) => {
conversationContext.venue_details = venueswithcuisines[0}["venue_details"];
conversationContext.cuisines = venueswithcuisines[1]["cuisines"];
conversationContext.venueType = venueType;
conversationContext.venues = venuesJson.venues;
continueConversation(request, response, context);
});
The problem with above implementation is, if for some reason getVenuesWithCuisine completed first before searchVenues the continueConversation is getting invokes making conversationContext.venues = venues as null.
How can i make these API calls synchronous so that the second API call is made only after the first one returns data.
You can use Promise.all instead of doing it synchronously. That will resolve once both of your promises resolve and give you the results.
http://bluebirdjs.com/docs/api/promise.all.html
Promise.all([searchVenues(venueType), getVenuesWithCuisine()]).then(function([venueResp, cuisineResp]) {
...
});

How to chain promise in array

I need help with ES6 Promises chaining in array processing.
How to process/define each item of array which goes into Promise.all method, when there is other async method inside resolve?
Here is simplified example:
function getData(data, callback) {
let groupPromises = data.map(row => {
var coordinates = getCoordinates(row);
return Promise.resolve({
"place": getPlaces(coordinates), //how to invoke this method
"data": row
};
});
Promise.all(groupPromises)
.then(groups => callback(groups))
.catch(err => console.log(err));
}
}
function getPlaces(coordinates) {
return new Promise(function(resolve, reject) {
if(coordinates == null) {
reject();
}
parameters = {
location: [coordinates.latitude, coordinates.longitude],
rankby: "distance",
};
googlePlaces.searchPlace(parameters, function (error, response) {
if (error) {
reject(error);
};
resolve(response);
});
}
}
You can do it like this where you add a .then() handler to your first promise that gets the place and then when that's available returns the object you want. The resolved results of your Promise.all() will then be the array of objects you want:
function getData(data, callback) {
let groupPromises = data.map(row => {
var coordinates = getCoordinates(row);
// add .then() handler here to convert the place result
// into the object you want it in
return getPlaces(coordinates).then(place => {
return {place: place, data: row};
});
});
return Promise.all(groupPromises)
.then(groups => callback(groups))
.catch(err => {
console.log(err);
throw err;
});
}
}
function getPlaces(coordinates) {
return new Promise(function(resolve, reject) {
if(coordinates == null) {
reject();
}
parameters = {
location: [coordinates.latitude, coordinates.longitude],
rankby: "distance",
};
googlePlaces.searchPlace(parameters, function (error, response) {
if (error) {
reject(error);
};
resolve(response);
});
}
}
FYI, since you're converting over to promises, why not just return the promise from getData() and not use a callback there at all? Your current code has no way of communicating back an error from getData() which is something that comes largely for free with promises.
In fact with pure promises, getData() could be simplified to this:
function getData(data, callback) {
return Promise.all(data.map(row => {
return getPlaces(getCoordinates(row)).then(function(place) {
return {place: place, data: row};
});
}));
}

Trouble to synchronise promises in Node.js using Q

I am currently doing an API in Node.JS with the framework Sails.js. I am using promises for the first time and I have some troubles to sync my promises like I want.
My main function is the following :
createCard: function(req, res) {
checkIfUserHasStripeAccount(req.user)
.then(addCreditCardToStripeAccount())
.then(function cardCreated() {
res.send(200, {
msg: 'Card created'
});
})
.catch(function handleError(err) {
res.send(err.httpCode, err.msg);
})
},
Obviously I can't add a credit card to a stripe account if the user doesn't have one.
The function checkIfUserHasStripeAccount() checks if the account exists and if not, create it.
Here is the code for this part :
function checkIfUserHasStripeAccount(user) {
var deferred = q.defer();
if (!user.idStripe) {
createStripeAccountToUser(user)
.then(function(savedUser) {
deferred.resolve(savedUser);
})
.catch(function(err) {
deferred.reject(err);
})
} else {
deferred.resolve(user);
}
return deferred.promise;
}
function createStripeAccountToUser(user) {
var deferred = q.defer();
var jsonUserToCreate = {
description: user.firstname + ' ' + user.surname,
email: user.email
};
stripe.customers.create(jsonUserToCreate, function(err, customer) {
if (err) {
deferred.reject({
httpCode: 500,
msg: 'some error'
});
} else {
user.idStripe = customer.id;
user.save(function(err, savedUser) {
if (err) {
deferred.reject({
httpCode: 500,
msg: 'some error'
});
}
deferred.resolve(savedUser);
});
}
});
return deferred.promise;
}
The problem is that the .then(addCreditCardToStripeAccount()) is executed before checkIfUserHasStripeAccount() is finished.
I can't figure out why. I thought the .then(addCreditCardToStripeAccount()) would only be executed if it received a reject or resolve.
You are correct in your line of thought.
The problem is that you are invoking your function instead of referencing it:
.then(addCreditCardToStripeAccount())
should be:
.then(addCreditCardToStripeAccount)
I expect this to work:
createCard: function (req, res) {
checkIfUserHasStripeAccount(req.user)
.then(addCreditCardToStripeAccount)
.then(function cardCreated(){
res.send(200, {msg: 'Card created'});
})
.catch(function handleError(err) {
res.send(err.httpCode, err.msg);
})
},
For future, note that the () after the function name invokes the function, as order of execution in JS will evaluate it first due to being inside the then's ().
In promise chains, always invoke only the first function. Example:
function first () { /*...*/ } // All return promise.
function second() { /*...*/ }
function third () { /*...*/ }
first() // Invoked
.then(second) // Not invoked. second() will have been bad here.
.then(third);

Resources