39295

Return Promises instead of Async

Question:

I want to return data which will look something like this

[ shops: { id, products:[{Object}, {Object}] }, shops: { id, products:[{Object}, {Object}] } ]

So I have list of shop ids on which I need to iterate and get the products for each shop and append it and then return.

This is how I have done

const shops = response.data const respToSend = [] async.eachSeries(shops, (shop, callback) => { const id = shop.shop_id const shopObj = { id } // Fetch products of each Shop. getProductList(id) .then((responseData) => { shopObj.products = responseData.data.products respToSend.push(shopObj) callback() }) .catch(() => { }) }, () => { return respToSend })

As you can see that I am using async node module for doing the async task but the application requires from me to return response as Promises which will then be resolved otherwise it prints null. How can I achieve this using promises? I am new to promises so sorry for any typo.

Answer1:

try <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all" rel="nofollow">Promise.all(iterable)</a>

Promise.all(shops.map(shop => getProductList(shop.shop_id) .then(products => ({id: shop.shop_id, products})) .then(/* actions for each shop */)) .then(shops => { /* actions for array of shop objects */ respToSend = shops; })

Recommend

  • How to make an array of Deferred objects
  • HasMany, BelongsTo for price comparision problem
  • Divide the cell content into separate rows in Excel 2010
  • Auto turn off toggle button when other one is turned on
  • C++ std::auto_ptr copy constructor
  • Hibernate criteria query
  • is there a way to update filter with async data
  • Authentication failed with Azure Active Directory in Windows Phone
  • In loopback documentation what does variable 'cb' stands for?
  • Defined variables not working in javascript files when I use getScript
  • Can't remove headers after they are sent
  • ThreadStatic in asynchronous ASP.NET Web API
  • How to synchronize jQuery dialog box to act like alert() of Javascript
  • JPA flush vs commit
  • htaccess rewriting URLs with multiple forward slashes
  • jquery mobile loadPage not working
  • Apache 2.4 - remove | delete | uninstall
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Is there a mandatory requirement to switch app.yaml?
  • File upload with ng-file-upload throwing error
  • How to delete a row from a dynamic generate table using jquery?
  • Python: how to group similar lists together in a list of lists?
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • AngularJs get employee from factory
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Free memory of cv::Mat loaded using FileStorage API
  • Trying to get generic when generic is not available
  • Getting Messege Twice Using IMvxMessenger
  • Change div Background jquery
  • How to get Windows thread pool to call class member function?
  • Bitwise OR returns boolean when one of operands is nil
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?