TMSWEB Core "how to get the value stored in [[PromiseResult]]"

Hello,
I have a very simple Javascript code below which is an example of "Asynchronous JavaScript". I failed to store the outcome of PromiseResult into a variable,
to be used later. There are a lot of similiar cases, ( Promise), in tensorflow.js that I wanted to use all goodies of tensors in .js with {asm.. end} block. TJSPromise class in JS has no function that returns PromiseResult. How one can solve this problem in TMSWEB Core?
Regards and Thanks for Help.
Ertan

function TForm1.SimplePromise2: TJSPromise; assembler;
asm
let lotteryOutcome;
const lotteryPromise = new Promise(function (resolve, reject) {
console.log('Lotter draw is happening :crystal_ball:');
setTimeout(function () {
if (Math.random() >= 0.5) {
lotteryOutcome ='You WIN';
resolve(lotteryOutcome);
} else {
lotteryOutcome ='You lost your money';
reject(new Error(lotteryOutcome));
}
}, 1000);
});

return lotteryPromise
.then(res => lotteryOutcome)
.catch(err => lotteryOutcome);
end;

procedure TForm1.WebButton1Click( Sender: TObject );
var
res: TJSPromiseResolver;
rej: TJSPromiseResolver;

begin

console.log( ' 1: Asynchronous JavaScript' );
console.log( ' 2: Before call promise');
console.log(SimplePromise2._then(res,rej));
// on Crome console above prints
//Promise {}
//[[Prototype]]: Promise
//[[PromiseState]]: "fulfilled"
//[[PromiseResult]]: "You WIN"
// want to get PromiseResult and store it to variable to be used later.

end;

//WebButton1Click cliked 2 times => outputs :
1: Asynchronous JavaScript
2: Before call promise
Lotter draw is happening :crystal_ball:
Promise {}
[[Prototype]]: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: "You WIN"

1: Asynchronous JavaScript
2: Before call promise
Lotter draw is happening :crystal_ball:
Promise {}
[[Prototype]]: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: "You lost your money"

Hello again,
After reading and searching further about promise, I managed to find a solution to my problem with await call back function. But there must be a way to extract [[PromiseResult]] value from promise object, but how?

type
TForm1 = class( TWebForm )
[ async ]
function Promise_02(): JSValue; assembler;
// ******************************

function TForm1.Promise_02: JSValue;
asm
function promiseFunc() {
var promise = new Promise( (resolve, reject) => {

 if (Math.random() >= 0.1) {
    let result = 'You WIN';
    resolve(result)
}else {
    let  result = 'You lost your money';;
    reject(result);
}

})
return promise;

}

let result_as_value = promiseFunc()
.then(result => (result_as_value = result))
.catch(result => (result_as_value = result));

return result_as_value;

end;
// ******************************
procedure TForm1.WebButton1Click( Sender: TObject );
var
aVal: JSValue;
begin
console.log( ' 1: Asynchronous JavaScript' );
console.log( ' 2: Before call promise');
aVal := await(Promise_02);
console.log('3: Next Line should be aVal');
console.log(aVal);
end;

// ******************************
output:

1: Asynchronous JavaScript
2: Before call promise
3: Next Line should be aVal
You WIN
1: Asynchronous JavaScript
2: Before call promise
3: Next Line should be aVal
You lost your money

resolved!

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.