0

I am trying to understand when you would use the "RETURN session.run" versus the "session.run" in a nested promise (in JavaScript). I have seen examples of both but I am not following the logic for the different usage. I have the following code(pseudo) as an example. I am using Neo4j for my database:

fn=function(data,callback)
{
session
.run(cypherquery)
.then function(result)
 {
  return session  //why a return here...the code seems to work without it!
   .run(anothercypherquery1)
   .then function(result)
     {
      for (var i=0; i<result.somearray.length;i++)
       {
        session  //why wouldn't I use a Return here? or should I?
        .run(anothercypherquery2)
         {}
         .then(function(result){})
         .catch(function(err){})
       }
     }
   .catch( function(){})
 }
.catch(function(){})
Return cb();
} 

Each session is dependent on the previous one to complete before proceeding but the return data from the cypherquery is not needed. the scenario would be similar to creating a new record with relationships where first cypher check if record exist already....second cypher create new record...third cypher create relationships in a for loop. I am trying to determine the best way to do this and why? any help would be much appreciated.

MichaelE
  • 757
  • 14
  • 42
  • You can substitute using `Promise.all()`, `Array.prototype.map()` or `.reduce()` for `for` loop, recursion; see http://stackoverflow.com/questions/38034574/multiple-sequential-fetch-promise – guest271314 Sep 24 '16 at 02:35
  • `why wouldn't I use a Return here?` - because then the function would return in the first iteration - P.S. the code you posted is not valid javascript at all ... it's `.then(function(result) { })` not `.then function(result) { }` – Jaromanda X Sep 24 '16 at 04:28
  • `why a return here...` - in case you want to chain this promise ... without a return, the `.then` returns a Promise that is resolved to `undefined` - with the return, `.then` returns the value returned by `session.run` - if that's a Promise, it returns that promise, if it's some value instead, it returns a Promise resolved with the value – Jaromanda X Sep 24 '16 at 04:30
  • `the following code(pseudo)` - if you need help with pseudo code, you'll get pseudo help – Jaromanda X Sep 24 '16 at 04:32
  • @Jaromanda....thanks for the assist. I wasn't looking help with the code just the concept...I am sure despite the obvious JavaScript syntax error my intent is clear. What I understand you to be saying is that if I want data from my .run I must use the Return....and if I am chaining and need the data in the next chain I must also use the return. But if I don't need the data to continue I can ignore the Return....is this correct? – MichaelE Sep 24 '16 at 10:08

1 Answers1

1

Assuming that your for loop collects independent promises than you could use Promise.all() as follows;

fn = function(data,callback){
      session.run(cypherquery)
             .then(function(result){
                     return session.run(anothercypherquery1);
                   })
             .then(function(result){
                     var promises = [];
                     for (var i=0; i<result.somearray.length;i++) promises[i] = session.run(anothercypherquery2);
                     return Promise.all(promises);
                   })
             .then(function(result){
                     callback(result);
                   })
             .catch(function(err){});
     };
Redu
  • 25,060
  • 6
  • 56
  • 76
  • N@Redu....I like the elegance of this answer I will have to test it....but it doesn't explain why. ....Also what if I didn't need result.somearray.length to give me the length of the array....I got it from req.body.checkboxes....so in fact I don't need any of the values coming from the db in the next session? – MichaelE Sep 24 '16 at 10:16
  • @MichaelE It explains in the sense of, once you receive a "promise or a result" within your `then` block which is needed for the "next" async operation you should just `return` it and handle the next async operation in the next `then` stage. Promise is just a functional abstraction of implementing async operations without nesting but chaining them one after the other by using flat `then` stages. After several async operations, once you receive your final result or promise, you are expected to `return` it to a final `then` stage and perform your desired callback operation there. – Redu Sep 24 '16 at 10:51
  • I have tested your answer and accepted it. Your explanation is still a little foggy, especially since you use the words "in the sense of" but I am much further down the road in understanding the topic...Thanks – MichaelE Sep 24 '16 at 23:53