1

I'm trying to get my head round promises and so far it's very frustrating !

I am calling a method in the console that returns a promise like this :

getSharepointListItems("count")

The result of the call is :

Promise {<pending>}
__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: Array(1)
0: {ContentTypeId: SP.ContentTypeId, Title: "400000", _ModerationComments: null, File Type: null, ComplianceAssetId: null, …}
length: 1
__proto__: Array(0)

I want assign the 400000 to a variable and use it for further calculations, but im unable to capture it.

I read that i should chain a .then to my method call, which i understand also returns a promise. So i tried:

getSharepointListItems("PalletCounter").then( (r) => console.log(r))

And the result now is:

Promise {<pending>}
VM1315:1 
[{…}]
0: {ContentTypeId: SP.ContentTypeId, Title: "400000", _ModerationComments: null, File Type: null, ComplianceAssetId: null, …}
length: 1
__proto__: Array(0)

So the [[PromiseValue]]: Array(1) has now been replaced by what looks like an object in an array, but there is also a promise which is pending, which i don't understand.I'm still unable to get to the 400000 value/result. Chaining more .then does not make a difference. I still get the above result.

How do i go about retrieving the result and saving it to a variable?

thanks

Nick
  • 3,454
  • 6
  • 33
  • 56
  • You can ignore the first line of the output you get (Promise pending), as evidently you are evaluating this in the console, which *also* outputs the value of the whole expression, which is the promise returned by the `then` call. It is of no interest to you since you just want the output of the explicit `console.log`. If this were a proper program (not in console), you would only see the output of `console.log`. – trincot Oct 16 '19 at 20:17
  • You can replace `console.log(r)` with `console.log(r[0].Title)` to get the "400000". – trincot Oct 16 '19 at 20:19
  • @trincot thanks, i tried that and it seems to work in the console, but a pending promise is also present. When i run it in the application, instead of 400000, i get [OBJECT PROMISE] – Nick Oct 16 '19 at 20:28
  • Make sure to go through the referenced question. It has all you need. – trincot Oct 16 '19 at 20:32
  • @trincot i will. thanks for the tip. – Nick Oct 16 '19 at 20:33

0 Answers0