0

Let me explain my issue. I have a foreach loop and a switch condition inside it. In every switch case, I have concat the results in to a global array.

var resultArray = [];
Panels.forEach(function (panel, index) {
 switch( panel.CategoryId ){
   case 'math': 
      resultArray = resultArray.concat(SOMEVALUE COMES FROM DB);
   break;
   case 'physics': 
      resultArray = resultArray.concat(SOMEVALUE COMES FROM DB);
   break;
   case 'zoology': 
      resultArray = resultArray.concat(SOMEVALUE COMES FROM DB);
   break;
 }

 return resultArray;
})

But the output doesn't contain the values from all cases. I know its because of the asynchronous nature of nodejs. But how can we implement this function in to asynchronous?

Any ideas would be appreciable

2
  • Is the query performed before or after switch condition? Commented Mar 3, 2015 at 9:50
  • @MatteoRubini Query performed inside the case condition Commented Mar 3, 2015 at 9:55

3 Answers 3

0

To create a similar-sync program flow you must use a callback pattern in your design:

functionPerformingQuery(function(result){
    handleResult(result);
});

main-idea is:

  • create a function accepting a callback-function
  • call the callback-function when operation is completed
  • define callback-function inside your code to access all private variables

more standardized method is promise()

https://www.promisejs.org/

Sign up to request clarification or add additional context in comments.

Comments

0

in the foreach loop, there is no need in return. try it: var resultArray = []; Panels.forEach(function (panel, index) { switch( panel.CategoryId ){ case 'math': resultArray = resultArray.concat(SOMEVALUE COMES FROM DB); break; case 'physics': resultArray = resultArray.concat(SOMEVALUE COMES FROM DB); break; case 'zoology': resultArray = resultArray.concat(SOMEVALUE COMES FROM DB); break; } }) console.log(resultArray);

Comments

0

on my opinion, cuz the request on db is async, you need perform the result using a promise Jquery Promise

var resultArray = [];
Panels.forEach(function (panel, index) {
 switch( panel.CategoryId ){
   case 'math': 
      myDBfunction().then(function(result) {
         resultArray = resultArray.concat(result);
      });
   break;
   case 'physics': 
      myDBfunction().then(function(result) {
         resultArray = resultArray.concat(result);
      });
   break;
   case 'zoology': 
      myDBfunction().then(function(result) {
         resultArray = resultArray.concat(result);
      });
   break;
 }

 return resultArray;
})

function myDBfunction() {

    var deferred = new jQuery.Deferred();

    // do some with DB (not real code)
    mysql.query("SELECT * ....", function(dbError, dbResult) {
      deferred.resolve(dbResult);
    })

  return deferred.promise()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.