Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using results outside of async function

sorry if I ask this question here, but tried a lot of things and wasn’t able to solve this problem.

I am using this Library, I ran the example and all is good but when I
try to use the results outside the scope of the async function. like this:

let x;

const payload = {"Traffic":99,"Growth":9,"Transactions":9,"Weight":33};

let promise = new Promise((resolve, reject) => {
    decisionTable.execute_decision_table("Volumetrics", 
    decision_table,payload,  (err, results)  => {  
        resolve(results);   
    });
});

 x = promise.then(
    result =>{return (result) } 
);

console.log (x);

always got this: Promise { <pending> } rather than the json object

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I guess is my ignorance about promises and async functions. Any help would be much appreciated. Thank you.

>Solution :

You’d only be able to use it inside the function passed to then() like so:

promise.then(
    result => console.log(result) 
);

If you were going to do other cool stuff, you’d have to do it all in there.

If you don’t like how that looks, look into the async/await syntax which is more readable in most cases like so:

let x = await promise;

console.log (x);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading