This is easier to do with async and await functions, I got it done quite quickly. However, I wanted to try doing it without async as I’m learning.
- I don’t understand the way data is fetched and applied in the “showPromiseScores” function where data is fetched and I try to append it as the “link” property of an object. Can you help me understand why this works inconsistently?
- Is it better to use async await, or not?
Here’s the code to get the data:
function getPromiseScores () {
let scoresForMember = currentMember.getMember()
.then( results => {
let r = wixData.query('Scores').eq("_owner", results._id).find();
return r;
});
return scoresForMember;
}
function getPromisePatternContext (p) {
const qResult = wixData.query('Patterns').eq("_id", p).find();
return qResult;
}
function showPromiseScores () {
let scoreResults = getPromiseScores()
.then( scoreRes => {
if (scoreRes && scoreRes.length > 0) {
let scoredItems = scoreRes.items;
let context;
for (let i = 0; i < scoreRes.length; i++) {
context = getPromisePatternContext(scoredItems[i].scoredPattern)
.then( data => {
scoredItems[i].link = data.items[0]['link-patterns-title'];
return scoredItems;
});
}
// ^^ THE LOOP CODE ABOVE SEEMS TO BE INCONSISTENTLY APPLIED TO THE DATA OUTPUT
console.log(context);
return context;
} else {
console.log("Getting pattern title didn't work");
}
});
return scoreResults;
}
Here you can see data is added to the “link” property seemingly randomly, sometimes all/some/none of the fetched data has the link data applied:
What am I doing wrong?