0

Considering the example:

function returnValue () {
   return somePromise.then (
     function (someThing) {
       return {
         sucess: true,
         data: someThing
       }
     },
     function (someError) {
       return {
         sucess: false,
         data: someError
       }
     }
   )
}

Console.log (returnValue ())

What should I do so that I actually have "someThing" or "someError"? And not a Promise pending?

Just to note ... when I write a code like this within "Meteor.methods" it works exactly as I would like, that is, it returns a value that I return to the client, but outside of "Meteor.methods" or in the client (browser, using or not any framework) the one I have is a Promise pending.

rogeriojlle
  • 1,046
  • 1
  • 11
  • 19

1 Answers1

0

The function passed to .then() returns results asynchronously. The Promise value of the fulfilled Promise will be available as the argument of the passed function. Console.log (returnValue ()), as you noted, logs the Promise itself, not the Promise value. Chain .then() to returnValue() call. Also, Console.log (returnValue ()) should be console.log().

let somePromise = Promise.resolve("abc");

function returnValue () {
   return somePromise.then (
     function (someThing) {
       return {
         sucess: true,
         data: someThing
       }
     },
     function (someError) {
       return {
         sucess: false,
         data: someError
       }
     }
   )
}

returnValue().then(function(result) {
  console.log(result)
})
guest271314
  • 1
  • 15
  • 104
  • 177
  • Right, but does not that take the value out of the promise, or should I accept that this is impossible from the browser? – rogeriojlle Dec 17 '16 at 10:55
  • @rogeriojlle Hesitant to state what is "impossible". Would suggest reading the specification [Promises/A+](https://promisesaplus.com/) _"A promise represents the eventual result of an asynchronous operation. The primary way of interacting with a promise is through its `then` method, which registers callbacks to receive either a promise’s eventual value or the reason why the promise cannot be fulfilled."_ See also [Promises](https://www.promisejs.org/), [You're Missing the Point of Promises](https://gist.github.com/domenic/3889970#youre-missing-the-point-of-promises). – guest271314 Dec 17 '16 at 17:14
  • @rogeriojlle See [What does \[\[PromiseValue\]\] mean in javascript console and how to do I get it](http://stackoverflow.com/questions/28916710/what-does-promisevalue-mean-in-javascript-console-and-how-to-do-i-get-it) – guest271314 Dec 17 '16 at 23:40