Hello,
I have created a promise (mypromise) in a backend module and call it from a function test in same backend module but the function is not waiting for promise to be returned … please tell me why. Here is the code :
var mypromise = new Promise(function(resolve,reject) {
resolve(‘sentfrompromise’);
});
export function test(a) {
console.log(‘begin function test’);
mypromise.then(resolve => console.log(resolve))
.then(console.log(‘after resolve’));
return(a);
}
And when I call the function I get in console following :
begin function test
after resolve
sentfrompromise
Which means code in second .then is executed before responce from Promise … please help me understand why ?
Thank you !
Hi,
you can also use asynch await it is a bit easier to reason about.
there is an issue with your promise scope, however this forum is for wix code specific issues.
i’d recommend you write the code sample above in any simple javascript environment you find easier to debug and then use it in the backend code in wix code.
there are good examples here: JavaScript ES 2017: Learn Async/Await by Example | by Brandon Morelli | codeburst
good luck!
Shlomi