I have two called functions that return promises and I would like to make it cleaner by possibly having on catch statement to catch the errors.
I think I am doing it the right way although the code does call the functions in the correct order asynchronously.
These are my calls:
// The text is hopefully the email address and the pin number
fb.verifyEmailPin(text).then(function(reply){
// Set the new state to 'get_city'
fb.setState(FB_ID).then(function(result){
}).catch(function(v) {
// Rejection
// If there was an error then prompt the user to enter again
}); // setState
}).catch(function(err){
});// verifyEmailPin
And this the actual function - for setState, I haven't yet written the code for the verifyEmailPin function but it follows the same structure as the set state in terms of passing back resolve or reject.
/*
* Function : setState
* Purpose : Set the state to what every is send in on the parameter
*/
exports.setState = function(fbid,newstate){
var success = 'Y';
return new Promise((resolve, reject) => {
client.hmset(fbid, { 'state': newstate });
// Check that we have set it ok
client.hmget(fbid,'state',function(err,reply){
if (err || reply != newstate) {
return reject(err);
}
return resolve(success);
});
}).catch(function(v) {
});
}