0

I think I got the idea of promise as it name speaks out, we can or can't have a result in return. To access the resolved value we need to call then on the promise. This is my catch.

I don't want to do anything with result, I just want to return value because it is an API function that I'm developing.

I have two promises like this:

var willWeGetConsumptions = new Promise(function(resolve, reject) {

var willWeGetPrices = new Promise(function(resolve, reject) {

and another consumer promise like this:

var finalPromise = Promise.all([willWeGetConsumptions, willWeGetPrices]).then(function(values) {

My thoughts are:

  1. Return from var finalPromise = Promise.all when done, (when resolve is called), Although I couldn't.
  2. Set Timeout function (which I think is possible but I don't know how). It should wait for resolved value and return directly. It should be in the same scope level as declared promises, and not defined inside one of them.

All other solutions are refering to logging on finalPromise.then(function(value) { console.log(value)});

I think it is clear now that I want a returned value, without further dealing with it.

Last edit: For those who didn't understand. Obviously, I don't want to user (caller of API) to deal with a Promise object (even always I assure that is resolved).

Why last edit? because I am now aware this is a wider problem, as referred like duplicate. it's true, the question is how to return from an Async function.

Curcuma_
  • 851
  • 2
  • 12
  • 37
  • why is the problem in using Promise.all ? – iwaduarte Apr 25 '18 at 10:00
  • If a value is returned, it will be encapsulated in a Promise object. and I can't access it unless I chain another time with then. (although I can't chain on client side if you see what I mean) – Curcuma_ Apr 25 '18 at 10:03
  • If a value is returned in `.then (function (values){ ...` will be an array with the desired output, from there you can save, update, call another function etc. I do not get your point. – iwaduarte Apr 25 '18 at 10:11
  • if you tried Promises, you can see that the return is a Promise object not a the intended value. :) – Curcuma_ Apr 25 '18 at 10:13
  • please check my answer, I think I clarify your doubt. – iwaduarte Apr 25 '18 at 10:21

2 Answers2

1

I think you are misunderstanding the concept of Promises.

From Promise.all mozilla The Promise.all(iterable) method returns a single Promise that resolves when all of the promises in the iterable argument have resolved or when the iterable argument contains NO promises. It rejects with the reason of the first promise that rejects.

        var promise1 = Promise.resolve(3);
        var promise2 = 42;
        var promise3 = new Promise(function(resolve, reject) {
          setTimeout(resolve, 100, 'foo');
        });

  function returningAPromiseForLater (){
       return Promise.all([promise1, promise2, 
    promise3]).then(function(values) {
          console.log(values);
        // Update not just logging.
         whateverFunctionYouCall ( values );
        });
        // expected output: Array [3, 42, "foo"]


   }

 **INVOKING AN EXTERNAL FUNCTION** 

   function whateverFunctionYouCall ( array ) {
        array.forEach(function(singleValue) {
            // send data to DB
           // create a File
            }
        }


**UPDATE 2: Getting the response later**

receiveThePromise = returningAPromiseForLater.then (function(valuesHEREasRESPONSE){
// do whatever with valuesHEREasRESPONSE variable.

});
iwaduarte
  • 1,600
  • 17
  • 23
  • aha, I think I understood that, but can we get the value of a promise instead of logging it, let's say after waiting for it. no more chaining. just block execution and wait, then get the value and do whatever with it. – Curcuma_ Apr 25 '18 at 10:24
  • I've updated my answer. Yes you can get the value and do whatever with it. – iwaduarte Apr 25 '18 at 10:32
  • ok, the thing is always you can't get values to outside scope like a normal return, I guess my question is a specific case of what was pointed as a duplicate question. returning from an async call. – Curcuma_ Apr 25 '18 at 11:34
  • if you want to return Promise.all in your function you need to use `then` in the caller function. See my update response. Also, if you can't show the code I will not know what exactly you want and will be pointless. – iwaduarte Apr 25 '18 at 11:58
1

Promise.all() is the right way to retun multiple promises from a promise. You may even add some synchronous values into the mix.

asyncFun = data => new Promise((v,x) => setTimeout(v, Math.random()*1000, data))

Promise.resolve(Promise.all([asyncFun(1), asyncFun(2), 3]))
       .then(([v1,v2,v3]) => console.log(v1 ,v2 ,v3));
Redu
  • 25,060
  • 6
  • 56
  • 76