Problem : Given the library method getUser (DB / Local storage fetch asynchronously), fetch all the user objects by ID  in the provided array and return an array consisting of all the requested user objects . It should return the response user object in same order like we are passing the IDs in array

Using promise approach to solve the problem

Example :

Input : [8,99,1,7,42]

Output :[{“id”:8,”name”:”Marut Kala”},{“id”:99,”name”:”Kumara Ganguly”},{“id”:1,”name”:”Shaamil Eid”},{“id”:7,”name”:”Awf Taheri”},{“id”:42,”name”:”Chandra Saini”}]

Here is the nodejs working code https://github.com/fuzonmedia/expressSmallCodeSample/blob/master/app1.js

Note :

  1. getUser returns a promise  &  it can be a DB search function for user object based on ID  .We used local array to store user objects
  2. For async purpose we used setTimeout to delay the execution.
  3. We dont need to think about return order , promise.each always return result based on input array order 
Promise | Bluebird | Promise chain | Loop | nested Promise – NodeJS implementation
Tagged on:             

2 thoughts on “Promise | Bluebird | Promise chain | Loop | nested Promise – NodeJS implementation

  • July 12, 2017 at 1:14 pm
    Permalink

    Hey there would you mind letting me know which web host you’re
    using? I’ve loaded your blog in 3 completely different browsers and I must say this blog
    loads a lot faster then most. Can you recommend a good internet hosting provider at a honest price?
    Cheers, I appreciate it!

    Reply
    • July 27, 2017 at 11:01 am
      Permalink

      Hey !

      Thanks for your words . You can try Digital Ocean

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *