Manaus
Manaus

Reputation: 451

Where do nested function parameters come from?

In the following code, where does the parameter chapters comes from? Same question for error, where does it come from? I'd say they are passed on from the function getChapters(), together with error, but how can I know? How can I see from the debugger what values does getChapters returns?

player.getChapters().then(function(chapters) {
    // chapters = an array of chapters objects
}).catch(function(error) {
    // error
});

Upvotes: 0

Views: 103

Answers (4)

Masood Rezazadeh
Masood Rezazadeh

Reputation: 64

it seems the getChapters method on player object returns a Promise (like a web api call).

as described in MDN

A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.

When you create a Promise there are two situation

  1. Your Promise will resolve so you return (resolve) the value.
  2. Your Promise will fail (for example with a HTTP error) so you return the error (reject)

when promise calls the resolve program will call the function in then with the result as param. when promise calls the reject program will call the function in catch with error as param.

you can see the flow in the following diagram: enter image description here

for debugging you can simply place a breakpoint inside catch or then functions and see the parameter value.

I highly suggest you study javascript Promises to grasp how this happens.

Upvotes: 1

Irfan wani
Irfan wani

Reputation: 5075

As this is a promise, so here the chapters is simply what the promise is going to return with successful call. I think this is checked by status code. If that is 200 then everything is ok else it passes an error to the catch method to handle that.

Upvotes: -1

Robin Zigmond
Robin Zigmond

Reputation: 18249

It looks like getChapter will return a Promise, which either "succeeds" (technically is "fulfilled") or fails (is "rejected").

In the former case the Promise will be fulfilled with a particular value, which will then be passed as the parameter (which you've chosen to call chapters) to the .then handler. If it's rejected, the "rejection reason", basically an error message, gets passed to the next .catch block in the chain - so in other words if getChapter fails, the .catch handler will be called with its parameter being passed the error message.

How can I see from the debugger what values does getChapters returns?

Depends on what you really want. You can always step into getChapters, which may give you insight into what task that performs and how success or failure is determined. Otherwise, you can simply put breakpoints inside both the .then and .catch handlers to see which one is called and what parameter it gets.

Upvotes: 3

Jonas Wilms
Jonas Wilms

Reputation: 138257

getChapters() returns a promise. When the Promise resolves or rejects, a value gets passed in which the .then and .catch callbacks then receive:

  new Promise(function(resolve, reject) {
    resolve("this value goes in");
  }).then(function(value) {
    // value === "this value goes in"
  })

From the debugger, when getChapters() executes you cannot know these values, as usually resolve gets called later in time, thus the value is not known yet, and the return value is a pending Promise. However when you place a breakpoint into the callback, you can see chapters and you will also find that value in one of the internal slots of the Promise.

Upvotes: 3

Related Questions