question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

concatMap with deferreds / chaining promises

See original GitHub issue

Any ideas on how I would do the equivalent of this in most?

http://jsbin.com/kodoni/2/edit?js,console

var source = Rx.Observable.of(1,2,3,4)
  .concatMap(function(x) {
    return Rx.Observable.defer(function() {
      return new Promise(function(resolve, reject) {
        setTimeout(function() {
          resolve(x);
        }, 1000);
      });
    });
  })
  .subscribe(function(i) {
    console.log(i);
  });

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:14 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
briancavaliercommented, Mar 17, 2015

Yay! 👍

0reactions
rektidecommented, Oct 6, 2016

Hey all, I had a similar but slightly different issue that this ticket helped me through- rather than trying to run fetch() multiple times on a list of urls, I was getting a stream of domainNames, and had a function that returned a promise with a list of records in the name. Finding out a way to flatten the results of that promise was a bit of a challenge for me- naively doing concatMap(n => lookupRecords(n)) returned the whole array as an element, but I wanted to flatten the resulting records out into the stream.

I used @briancavalier’s suggestion of unfolding to get by this, in slightly different form. It took me a pass or three to make right/not-ugly, so I built a library for the final version- most-promise-spread.

From the example there, I got mostPromiseSpread(Promise.resolve([1,2,3,4]).forEach(x => console.log(x*2)). For domainNames/records, it looks something like: domainNames.concatMap(n => mostPromiseSpread(lookupRecords(n)).

Read more comments on GitHub >

github_iconTop Results From Across the Web

rxjs - Is Observable from chained promises equivalent of ...
It's just how I've been doing things and I like it. What you do need to do is use a switchMap or concatMap...
Read more >
Resolve promises in sequence with RXJS (ConcatMap)
This article will be a short one mainly on resolving / handling promises in sequence using RXJS. One such example would be fetching...
Read more >
Using and chaining promises in AngularJS - AirPair
A guide to using promises in AngularJS to create multi-layered ... can be deferred further until a 3rd promise has been resolved/rejected, in...
Read more >
[Solved]-Is Observable from chained promises equivalent of ...
Coding example for the question Is Observable from chained promises equivalent of observables created with from and chained with concatMap?-rx.js.
Read more >
Async and Await | Hacker News
Most deferred objects like Futures in Java and Promises in other ... Introduce back-pressure in the protocol, and concatMap becomes safer ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found