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 ');
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
Promise {}
[[Prototype]]: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: "You WIN"
1: Asynchronous JavaScript
2: Before call promise
Lotter draw is happening
Promise {}
[[Prototype]]: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: "You lost your money"