How to avoid output arguments in Javascript

69 views
Skip to first unread message

GOKHAN DILEK

unread,
Jun 12, 2016, 4:34:24 AM6/12/16
to Clean Code Discussion
Hi,

I have 3 parameter function below.

function authenticateUser(username, password, callback) {
 userGateway
.verifyUser(username, password, function (data) {
 callback
(data);
 
});
}

I am trying to find a way to prevent the third parameter - the callback parameter in javascript.

Couple of problems with this function:
1- It has 3 parameters.
2- Callback parameter is output parameter which causes double take.

Anyone else had similar issue?


recep yalman

unread,
Jun 12, 2016, 7:57:06 AM6/12/16
to clean-code...@googlegroups.com
You can use promise if you wish.  

function authenticateUser(username,password){
    return new Promise(function(resolve,reject){ 
        userGateway.verifyUser(username,password, resolve);
     }
}


usage:

authenticateUser("aUserName","secret").then(function(data){
   //do something here 
})




--
The only way to go fast is to go well.
---
You received this message because you are subscribed to the Google Groups "Clean Code Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discu...@googlegroups.com.
To post to this group, send email to clean-code...@googlegroups.com.
Visit this group at https://groups.google.com/group/clean-code-discussion.

Łukasz Duda

unread,
Jun 12, 2016, 10:15:15 AM6/12/16
to Clean Code Discussion
You can group user name and password into credentials object.
var credentials = { userName: "abc", password: "def" };
authenticateUser(credentials, callback);
Reply all
Reply to author
Forward
0 new messages