0

I have the following flow to execute:

function doSomething(data, file) {
  createObjectOnDB(data).then(req =>
    upload(res.body.url, file).then(
      getResult(res.body.id)
    )
  })
}

Do you see the problem? I can't use .then to chain the upload and getResult because both of them are accessing the req object. Is there any workaround?

Thank you.

Amanda Ferrari
  • 1,168
  • 5
  • 17
  • 30
  • Did you mean for `getResult` to run after upload inside a callback? Right now you call `getResult` and pass that return value to `.then`. Might help to spell out exactly what order you'd want with pseudocode or something. – loganfsmyth Apr 12 '18 at 05:13
  • That's exactly it. I want run getResult after the upload. – Amanda Ferrari Apr 12 '18 at 05:14
  • I was declaring the first then as a `async` function, but that's a bad pattern, I believe – Amanda Ferrari Apr 12 '18 at 05:14
  • In your code `res` should be `req`? – Ori Drori Apr 12 '18 at 05:22
  • This whole topic in general covered here: [How to chain and share prior results with promises](https://stackoverflow.com/questions/28714298/how-to-chain-and-share-prior-results-with-promises/28714863#28714863) – jfriend00 Apr 12 '18 at 06:47
  • You don't need to chain on the outside, you can chain inside the handlers just fine. You just must not forget to `return` your promises from all your three functions. – Bergi Apr 13 '18 at 13:24

2 Answers2

2

Using promises you can add a .then() to the upload that will return req:

function doSomething(data, file) {
  return createObjectOnDB(data)
    .then(req => upload(req.body.url, file).then(() => req))
    .then(req => getResult(req.body.id));
}

An easier option is async/await:

async function doSomething(data, file) {
  const req = await createObjectOnDB(data);
  await upload(req.body.url, file);
  return await getResult(req.body.id);
}
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
-1

From "Rookie Mistake #1" in this article

A better style is this one:

remotedb.allDocs(...).then(function (resultOfAllDocs) {
  return localdb.put(...);
}).then(function (resultOfPut) {
  return localdb.get(...);
}).then(function (resultOfGet) {
  return localdb.put(...);
}).catch(function (err) {
  console.log(err);
});

This is called composing promises, and it's one of the great superpowers of promises. Each function will only be called when the previous promise has resolved, and it'll be called with that promise's output. More on that later.

coagmano
  • 5,542
  • 1
  • 28
  • 41
AkinsTech
  • 154
  • 6
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/19408698) – Vishal Chhodwani Apr 12 '18 at 05:37
  • Sorry, I got a bit lazy in my answer as I felt this was likely answered many times before. I should have extracted the answer from the site. Much better edit by Mr. Stark. – AkinsTech Apr 12 '18 at 05:49