Async await not returning value?


#1

I only see

Returned value is : 204 from within the function but not sure why this value is not returned back ?

import Promise from 'bluebird';
const callAsync = Promise.promisify(Meteor.call);

Meteor.methods({
    async testDoSomething() {
      const res1 = await callAsync('myFutureBasedFunction1'); // res1 is some xml
      const res2 = await callAsync('myFutureBasedFunction2', res1);  // res2 : result as expected
      const res3 = await callAsync('myFutureBasedFunction3', res2); // res3: status '204'
      const res5 = console.log('Returned value is : ', res2) ; // Success 204 only inside, but not returned?
      
      if (res4 == '204') {
        future.return('204');
     } else {
       future.return('Error');
    }

  return future.wait();
    },
});

var tmp = callAsync('testDoSomething') 

internally I get the res2 in my case as expected with a console.log

Returned value is : 204 

but res2 is not being returned by to the called function? But instead tmp is:

tmp is : { _bitField: 33554432,
I20160708-16:14:00.656(1)?   _fulfillmentHandler0: undefined,
I20160708-16:14:00.656(1)?   _rejectionHandler0: { _45: 0, _81: 0, _65: null, _54: null },
I20160708-16:14:00.656(1)?   _promise0: undefined,
I20160708-16:14:00.656(1)?   _receiver0: undefined,  
I20160708-16:14:00.657(1)?   _trace: { [Error] _parent: undefined, _promisesCreated: 0, _length: 1 } }

Or should I use Promise instead of Future even though this is called from within server to server? But eventually I will also call it from client

I just want to get the returned status code from outside the function, but it seems to be accessible only from within the function?


#2

Your code is somehow confusing, my guess is you have 2 promises one inside of the other.

In particular I suspec that last call tmp callAsync('testDoSomething') to be building a promise around a function that already returns a promise.

Meteor handles Promise, async and await out of the box. My advise is to rewrite your code without promisify so you can clearly see what is happening. Whenever you get back an object with underscore fiels like { _ … } it means you got back a promise.


#3

I read somewhere that the tail of the chain can be lost?

Is this what I am also doing as well?

http://exploringjs.com/es6/ch_promises.html

25.8 Common Promise chaining mistakes
25.8.1 Mistake: losing the tail of a Promise chain
In the following code, a chain of two Promises is built, but only the first part of it is returned. As a consequence, the tail of the chain is lost.

// Don’t do this
function foo() {
    const promise = asyncFunc();
    promise.then(result => {
        ···
    });

    return promise;
}

This can be fixed by returning the tail of the chain:

function foo() {
    const promise = asyncFunc();
    return promise.then(result => {
        ···
    });
}

If you don’t need the variable promise, you can simplify this code further:

function foo() {
    return asyncFunc()
    .then(result => {
        ···
    });
}

#4

Try replacing

var tmp = callAsync('testDoSomething')

with

const f = async () => {
  var tmp = await callAsync('testDoSomething')
  console.log(tmp)
}

f()

The idea being to check if callAsync is returning a promise inside of a promise as I suspect, or not.


#5

tmp is something like:
{ _45: 0, _81: 0, _65: null, _54: null }


#6

That means tmp is a promise.


#7

Sorry I am a bit confused @diegoolivier, isnt that what I expect, a promise which will eventually be fulfilled?

So if the cluster of awaits are chained, and the last one is capturing the ‘204’ in my case within the group of chained async functions, how come this is not returned back to the calling instance?

I would like the returned value (in my case ‘204’) not the promise which is what is returning.