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
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);