Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any way to convert array into individual values? #619

Open
rightaway opened this issue May 5, 2017 · 3 comments
Open

Any way to convert array into individual values? #619

rightaway opened this issue May 5, 2017 · 3 comments

Comments

@rightaway
Copy link

In this code, download returns an array of many objects, but handleSingleObject expects a single object to be piped through it. Is there a way to convert an array of objects into a single object for piping further down the chain?

highland(myArray)
  .map(page => highland(download(page))
  .parallel(5)
  .pipe(handleSingleObject())
  ...
@vqvu
Copy link
Collaborator

vqvu commented May 5, 2017

If you want to convert each array into a single object, use map. If you want to convert each array into multiple objects, use flatMap.

highland(myArray)
  .map(page => highland(download(page))
  .parallel(5)
  // This will flatten the arrays from download into multiple individual values.
  // Each one will be fed into handleSingleObject.
  .flatMap(_)
  .pipe(handleSingleObject())
  ...

@rightaway
Copy link
Author

Thanks! The documentation for flatMap also mentions sequence, if I have this code, it works as well. Is there any advantage to using flatMap over sequence when it's just an array of objects?

    await new Promise((resolve, reject) => {
      highland(myArray)
        .map(page => highland(download(page)))
        .mergeWithLimit(5)
        .sequence()
        .stopOnError(reject)
        .pipe(handleSingleObject())
        .pipe(fs.createWriteStream(someFile))
        .on('finish', resolve)
        .on('error', reject)
    })

Also I noticed something interesting. If I replace .sequence() above with .flatMap(highland()) (notice the extra (), it should just be .flatMap(highland) as in your example), I don't actually seem to get an error thrown from the await new Promise. Do I need to add/modify the error handlers in this code to make it throw in that case? Just hoping my code isn't silently ignoring errors without me realizing it.

@vqvu
Copy link
Collaborator

vqvu commented May 6, 2017

Is there any advantage to using flatMap over sequence when it's just an array of objects?

Sequence will work just as well for arrays.

Do I need to add/modify the error handlers in this code to make it throw in that case? Just hoping my code isn't silently ignoring errors without me realizing it.

It doesn't throw, because flatMap(highland()) is the same as map(highland()).sequence(), and map has a weird behavior where passing it a non-function value will cause it to map everything to that value. See the deprecation warning in the docs.

When you use .flatMap(highland()), it behaves the same as if you use

const stream = highland()
...
  .flatMap(() => stream)
  ...

I'm not sure what happens in this situation. You'll probably end up with a deadlock, where your promise never resolves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants