EdgeVerve / feel

Expression Language for creating and executing business rules in decision table based on DMN 1.1 specification for conformance level 3
MIT License
93 stars 48 forks source link

Using results outside of the async #35

Closed eljavero closed 11 months ago

eljavero commented 1 year ago

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

I am trying to use the results of the decision table 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.

kvsrohit commented 11 months ago

I believe you may have figured it out yourself by now. The Promise.then is called asynchronously and your console.log(x) is executed beforehand.

Either use the result inside the then callback

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

OR use async await syntax

 x = await promise;
console.log (x);