如何在嵌套的Promise中应用RETURN

时间:2021-05-29 18:04:31

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:

我试图了解何时在嵌套的promise(在JavaScript中)中使用“RETURN session.run”与“session.run”。我见过两者的例子,但我没有遵循不同用法的逻辑。我有以下代码(伪)作为示例。我在我的数据库中使用Neo4j:

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.

每个会话都依赖于前一个会话在继续之前完成,但不需要来自cypherquery的返回数据。该场景类似于创建具有关系的新记录,其中第一个密码检查记录是否已存在....第二个密码创建新记录...第三个密码在for循环中创建关系。我想确定最好的方法来做到这一点,为什么?任何帮助将非常感激。

1 个解决方案

#1


1  

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

假设你的for循环收集独立的promises,你可以使用Promise.all(),如下所示;

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){});
     };

#1


1  

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

假设你的for循环收集独立的promises,你可以使用Promise.all(),如下所示;

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){});
     };