Hi - I have run the code below in a browser, and in Node and it works perfectly. As soon as I put it into a JSW file and call it from a page the promises are all out of sync.
CLIENT CODE:
let ord = {
name: ‘order’,
items: [{name: “product1”, quantity:1}, {name: “product2”, quantity:1} ]
}
runOrder(ord)
.then((ordRes)=>{
console.log(order processed: ${ordRes.number}
);
})
SERVER CODE
function runOrder(order){
return new Promise((resolve, reject) => {
resolve(createOrUpdateOrder(order));
})
}
function createOrUpdateOrder(order) {
return new Promise((resolve, reject) => {
setTimeout(() => {
order.number = Math.random();
let promises = []
for (let i=0; i < order.items.length; i++){
let promise = createOrUpdateItem(order.items[i])
promises.push(promise)
}
Promise.all(promises).then(()=>{
console.log(‘orderLines - completed’)
resolve(order); //LAST THING I WANT RESOLVED/RETURNED
});
}, 100);
})
}
function createOrUpdateItem(item) {
return new Promise((resolve, reject) => {
setTimeout(() => {
getBoxNumber()
.then(box =>{
item.boxNum = box;
console.log("item with box: " + item.boxNum);
resolve(item);
})
}, 2000);
})
}
function getBoxNumber(){
return new Promise((resolve, reject) => {
setTimeout(() => {
let result = Math.random();
console.log(Box number : ${result}
);
resolve(result);
}, 10);
})
}
To add a interesting observations. When I include the code inline in WIX it works. When I put it in JSW it does not!!
I am doing this as a backend JSW. I have read all of those documents, but the issue is the promise-handling inside a JSW. When I include my code in the actual page JS it runs perfectly. When I add it to a JSW andn call it from a page the calls appear out of sync.
@givemeawhisky This doesn’t relate to the question. The promises are working fine - just not inside a JSW file.
@mike63762
Well if it doesn’t work when you put it through the backend jsw file, then that must mean your code in the jsw backend file is wrong.
If you are simply just pasting the server code that you have posted above straight into your backend jsw file, then it won’t work as it is coded for the jsw file.
@givemeawhisky Sorry I’m not sure I follow. The server code above is the JSW file. The Client code imports that JSW file and runs it by executing: runOrder function.
If I take the same backend code and put it all in one page everything works. So the problem appears to be with the JSW itself. I had read on another post something about a similar problem, but didn’t follow the answer. I believe they were suggesting to insert a function that returns a function - which is what’s I’ve done with runnOrder
@givemeawhisky - Here is the post I was referencing…
@mike63762
Yes, however look at the way that Andreas has written that backend jsw file, the same with the examples shown in these pages.
https://support.wix.com/en/article/corvid-calling-server-side-code-from-the-front-end-with-web-modules
https://support.wix.com/en/article/corvid-tutorial-sending-an-email-on-form-submission
https://www.wix.com/corvid/reference/wix-fetch.html
You need to write your backend jsw file similar to as how they are all written along with the calls to them from your page code etc.