0

I want to remove one or two returns in the following .then() functions:

 return store.findParent(to.params.id).then((project) => {
    return store.findByParent('project', project).then((result) => {
      return {
        project: project.toJSON(),
        tasks: result
      }
    })
  })

I tried this:

  return store.findParent(to.params.id).then((project) => ({
    store.findByParent('project', project).then((result) => {
      project: project.toJSON(),
      tasks: store.findListByParent('project', project)
    })
  }))

But I get

Parsing error: Unexpected identifier at tasks: store.findListByParent

What's the proper way of doing it?

alex
  • 7,111
  • 15
  • 50
  • 77
  • I'm a bit surprised you removed the body `{}` from the inner function, but not the outer function. You also added `(...)` to the outer function and not the inner function. In short: You converter both functions in different ways. Why? – Felix Kling Jan 27 '16 at 22:10

1 Answers1

1

You want to get rid of braces and parenthesis when using the concise form of arrow functions - unless you want to return an object literal.

return store.findParent(to.params.id).then(project =>
    store.findByParent('project', project).then(result => 
        ({
            project: project.toJSON(),
            tasks: result
        })
    )
);

What you currently have is an object literal where the .findByParent… is a syntax error in the property name.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    Depends on how familiar you are with the syntax :-) I guess the own line for the object isn't a bad idea, but I definitely would flatten the pyramid and not use parenteses around single arguments – Bergi Jan 27 '16 at 15:17
  • Thanks, I agree with removing the parenthesis. As for the flattened version, I like it, but it doesn't work since `.then(result => ({` isn't waiting for the result of `store.findByParent()`...at least that's my theory. I get `"project" is not defined > project: project.toJSON(),` when I use that code. – alex Jan 27 '16 at 15:56
  • 1
    Well, it does wait, as `findByParent` returns a promise, but I was totally blind and overlooked the inner callback was a closure that accessed `project`. Have a look at [this thread](http://stackoverflow.com/q/28250680/1048572) for all possible ways to simplify :-) – Bergi Jan 27 '16 at 16:09
  • Okay, I'll check that. Thanks! – alex Jan 27 '16 at 16:12