0

This is not a duplicate question, this is an attempt to get a very specific detail from the exhaustive explanation at How do I return the response from an asynchronous call? which is not covered.

I don't quite understand the abstruse answers to resolving Promises.

I'm attempting to understand how to unwrap my data from a returned Promise. console.log(data) doesn't answer the question. "How do I get my payload?".

Why is is this a bloody secret?

I used node REPL to track my coding. I'm doing everything right except the last step, and I'm punching in the dark here.

someasyncFunction()
.then(data => data);

is supposed to return the "unwrapped" payload right? So why am I getting

$ node gettest.js 
Promise { <pending> }

There is a technical piece I am missing and I can't seem to get any help answering that last piece.

const https = require('https');
const datastore = {};

async function getInfo (){
        https.get('https://www.gnu.org/software/bash/manual/html_node/Command-Line-Editing.html#Command-Line-Editing', (resp) => {
        let data='';

        resp.on('data', (chunk) => {
            data += chunk;
        });

        resp.on('end', () => {
            //console.log(data);
            return data;
                        });
        }).on("error", (err) => {
            console.log("Error: " + err.message);
        });
}

datastore.info = getInfo().then(function (val){
    return val;
});

console.log(datastore.info);
Ken Ingram
  • 1,538
  • 5
  • 27
  • 52

1 Answers1

0

getInfo is a Promise, also the method then returns a Promise itself.

You are assigning your variable to a Promise, so obviously you'll get a reference of it.

As the question that you linked said, you should embrace the asynchronous nature of JavaScript, and your flow is just synchronous from the top to bottom. It's just asynchronous inside the getInfo function, but that's not enough since out of it the flow remains synchronous.

I advise you, just to understand what you are doing, to don't use async/await, since it lets you think that everything is synchronous while it's not.

So only use the val value inside the then function:

getInfo().then(function (val){
    // Your async code here
});

Once you are confident with this way of thinking, you can refactor it using async/await, which is almost syntactical sugar to have a better looking code:

(async function () {
  async function getInfo () {
    // your code
  }

  datastore.info = await getInfo();

  console.log(datastore.info);

})();
  • Wow. Not what I expected. so Promises are not an element that I can use within a synchronous process? I have to fit all the code inside an asynchronous container? – Ken Ingram Jun 29 '19 at 10:06
  • "only use the val value inside the then function"? So isn't that what I did? – Ken Ingram Jun 29 '19 at 10:12
  • This seems far afield of what I understood in the long expalantion of async. – Ken Ingram Jun 29 '19 at 10:13
  • 1. No, you haven't to fit all the code inside an async container, but there must be an async function somewhere. 2. No, you tried to use the `val` value inside the console.log, you renamed it as `datastore.info`, but you supposed it was the `val` value. – Christian Vincenzo Traina Jun 29 '19 at 20:50
  • async/await is awesome, trust me, but it's advanced stuff. Just stick with promises – Christian Vincenzo Traina Jun 29 '19 at 20:50
  • Ok. I've meditated on it a bit and I think I understand what I missed. The code running in the promise or async function is holding up everything. Nothing is complete until the Promise/async is complete. That means all code associated has to be wrapped in an async function or Promise. When the promise is resolved, the process is complete.It's like completion of a project depends on budget confirmation, but there's enough money to run the project to a certain point, after which nothing else can be done until the new budget is approved. – Ken Ingram Jun 30 '19 at 07:14
  • @KenIngram this is the problem with modern javascript tutorials! Doesn't exist a thing as "async function" at runtime, that's just syntax. There are only Promises – Christian Vincenzo Traina Jul 01 '19 at 08:06
  • Is my understanding on the right track though. That's the important part of my question. – Ken Ingram Jul 01 '19 at 17:28