Thursday 20 April 2017

return a single array with users, post, comments using bluebird

I want to return a single array which consists set of users,posts and comments

Structure of nested array should be somewhat :

  Users
      Posts [List of post created by user]
           Comments [ List of comments on that post]   

What I tired is

METHOD 1 :

 var result=[];

 Promise.map(getUsers(),(user)=>{
    return getPosts(user.userid)
           .then((post)=>{return getComments(post.postid)})
 })

METHOD 2 :

Promise.join(getUsers(),getPosts(),getComments(),function(users,posts,comments){
     var results=[];

     // May be here by iterating through users,posts,comments array 
      I can create nested array as required but still running for loop so many times  for each record is not right 
     also no userid and posttid are being passed to functions as params so there would be lot of iterating.
 })

but now I am confused how can I create a single array from it also I dont know if Promise.map is used in a right way or should I use Promise.join but I cannot pass respective ids to it .



via Vibhas

No comments:

Post a Comment