0

I'm using the RSVP.js lib in a browser.

I have one promise applicationReady

I have another promise loadSomeData

I have a final promise, configureUI

Each relies on the previous promise to do it's work. How do I get these three promises to run in series? I'm clearly missing something.

Thanks!

SOLUTION:

Ok, here's the answer:

Does not work:

applicationReady
.then(loadSomeData)
.then(configureUI)

Does work:

applicationReady
.then(function() { return loadSomeData; })
.then(function() { return configureUI; })

There is a difference between a promise and a function that returns a promise. Bummer that then() doesn't figure this out itself. What's the usecase for then(promise) ?

Michael Cole
  • 15,473
  • 7
  • 79
  • 96

2 Answers2

1

I have one promise, I have another promise, I have a final promise. Each relies on the previous promise to do it's work.

That makes no sense. If you have a promise already, it means that you have had started all three processes whose future results you are now holding in hand. Whether that process relies on some other promise (or not) is not in your responsibility - you only have the results.

How do I get these three promises to run in series?

You cannot "run" a promise. A promise only represents a result. You can however run a function. By saying "relies on the previous work", you mean that the results of the previous promise are passed into the function to run (and create the next "dependent" promise) as an argument - and that's just what .then() does.

What's the usecase for then(promise) ?

There is none. If you don't pass a function, then does nothing.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Ok, here's the answer:

Does not work:

applicationReady
.then(loadSomeData)  // loadSomeData is a promise
.then(configureUI)   // configureUI is a promise

Does work:

applicationReady
.then(function() { return loadSomeData; })
.then(function() { return configureUI; })

There is a difference between a promise and a function that returns a promise.

Michael Cole
  • 15,473
  • 7
  • 79
  • 96