Having problem in doing operations on values after asynchronous calls - javascript

Here i'm calculation two values color and number after calculation i'm updating the values but this is not working
gameSchema.methods.computeBets = async function(){
const game = this;
const greenMultiplier = 2;
const voiletMultiplier = 4.5;
const redMultiplier = 2;
try {
const { gameType } = game;
let model = Schema;
gameType === 'Fast Parity' ? model = FastParity : model = SlowParity;
const gameDetails = await model.findOne({ gameId: game._id });
const { allPlayerBettedBetId } = gameDetails;
let color = {};
let number = {};
await Promise.all(allPlayerBettedBetId.map(async(bet) => {
const betdetails = await UserBet.findById(bet._id);
const { colorBettedOn, numberBettedOn,amountBetted,betType} = betdetails;
// console.log(colorBettedOn, numberBettedOn, amountBetted);
if(betType==='color')
color[colorBettedOn] = color[colorBettedOn] ? color[colorBettedOn] + amountBetted : amountBetted;
if(betType==='number')
number[numberBettedOn] = number[numberBettedOn] ? number[numberBettedOn] + amountBetted : amountBetted;
}));
console.log(color, number);
color.forEach(item => {
if (item.green)
item.green *= greenMultiplier;
if (item.red)
item.red *= redMultiplier;
if (item.voilet)
item.voilet *= voiletMultiplier;
});
let colorCombination = {
"green": color.green,
"green/voilet": color.green+(color.voilet/2),
"red/voilet": color.red+(color.voilet/2),
"red": color.red
};
number.forEach(item => {
item.value *= 2;
})
console.log(number,colorCombination)
return { number, colorCombination };
after await Promise.all console.log(number,color) is working but when i'm updating the values they are not working.I'm not good at async function as i don't have much exprience in that.what mistake i'm doing in the forEach functions

Related

web worker and asynchronous operation

I am learning web worker and right now I am going through the problem of using await inside the onmessage. How can i do this?
import { Copc, Key } from "copc";
var nodePages, pages, receivedData;
async function load() {
let filename = "https://s3.amazonaws.com/data.entwine.io/millsite.copc.laz";
const copc = await Copc.create(filename);
let scale = copc.header.scale[0];
let [x_min, y_min, z_min, x_max, y_max, z_max] = copc.info.cube;
let width = Math.abs(x_max - x_min);
let center_x = (x_min + x_max) / 2;
let center_y = (y_min + y_max) / 2;
let center_z = (z_min + z_max) / 2;
receivedData = await Copc.loadHierarchyPage(
filename,
copc.info.rootHierarchyPage
);
nodePages = receivedData.nodes;
pages = receivedData.pages;
postMessage(200);
}
onmessage = function (message) {
let index = message.data;
let myRoot = nodePages[keyCountMap[m]];
const view = await Copc.loadPointDataView(filename, copc, myRoot);
};
and again there is another issue, the loadPointDataView function is asynchronous, how can i implement this in my webworker?
Any help please
This is my original code that i want to parallelize:
let filename = "https://s3.amazonaws.com/data.entwine.io/millsite.copc.laz";
const copc = await Copc.create(filename);
scale = copc.header.scale[0];
const { nodes: nodePages, pages: pages } = await Copc.loadHierarchyPage(
filename,
copc.info.rootHierarchyPage
);
for (let m = 0; m < keyCountMap.length; m += 2) {
let myRoot = nodePages[keyCountMap[m]];
const view = await Copc.loadPointDataView(filename, copc, myRoot);
let getters = ["X", "Y", "Z", "Intensity"].map(view.getter);
let chunkCount = 20;
let totalCalled = 0;
let innerPromises = [];
for (let j = 0; j < keyCountMap[m + 1]; j += chunkCount) {
readPoints(index + j, getters)
}
}
const readPoints = async (id, getters) => {
return new Promise((resolve, reject) => {
let returnPoint = getXyzi(id, getters);
positions.push(
returnPoint[0] - x_min - 0.5 * width,
returnPoint[1] - y_min - 0.5 * width,
returnPoint[2] - z_min - 0.5 * width
);
const vx = (returnPoint[3] / 65535) * 255;
color.setRGB(vx, vx, vx);
colors.push(color.r, color.g, color.b);
});
};
function getXyzi(index, getters) {
return getters.map((get) => get(index));
}

How can get totalBalanceAmount value from getSavingsInput() and put into (HERE)?

How can get totalBalanceAmount value from getSavingsInput() that i can put into (HERE)?
function getSavingsInput() {
let totalBalance = document.getElementById('balance')
let totalBalanceAmount = parseFloat(totalBalance.value)
return totalBalanceAmount;
}
document.getElementById('Savings').addEventListener('click',function() {
let savingInput = getInput('savigns-input') // this is the another function
let savingsInputAmount = parseFloat(savingInput)
const savignsAmount = (HERE) % savingsInputAmount;
const savedAmount = document.getElementById('saved-amount')
savedAmount.innerText = savignsAmount;
})

dealing with asynchronous updating of an object

const globalObject = {};
const varAList = [.....];
const varBList = [.....];
const varA = await Promise.all(
varAList.map(
async (aObject) => {
const toReturn = [];
for (const bObject of varBList) {
if (!globalObject[bObject.id]) {
console.log(`getting ${bObject.id}`);
globalObject[bObject.id] = await getObject(bObject.id);
}
if (globalObject[bObject.id].important == true){
toReturn.push(globalObject[bObject.id].something)
}
}
return {aObject.id: toReturn}
}
)
);
The problem with this code is that the line console.log(``getting ${bObject.id}``) will be repeated for the same id since the varAList is going in parallel. How do I make sure that I am asynchronously checking this object and not remaking a request if it already exists?
Any solutions?
You question is confusing, If understand your correctly, You don't want to iterate over varAList?
const globalObject = {};
const varAList = [.....];
const varBList = [.....];
async function* genarateResult() {
for (const bObject of varBList) {
let aObject = varAList.shift();
let obj = globalObject[bObject.id];
let toReturn = [];
if (!obj) {
console.log(`getting ${bObject.id}`);
globalObject[bObject.id] = await getObject(bObject.id);
}
if (obj.important == true) {
toReturn.push(obj.something)
}
yield { [aObject.id]: toReturn };
}
}
for await (const val of genarateResult()) {
console.log(val);
}
Does it answer you question? or what I missed?

Why is my function not returning the desired value, however, console.log() does the job?

I know this question might be a little basic but I think I am lacking some important fundamental concept. I am coding in node.js, and I have a function getPostInfo(). In the end, I have a return statement that returns the object that I created. However, when I run it on PowerShell I get no output. Furthermore, when I console.log(object) I get the required answer.
If someone knows what I might be doing wrong, let me know.
P.S. - The major chunks of code in the middle can be skipped as they are just to get information of a webpage
const cheerio = require('cheerio');
const axios = require('axios');
let object = {};
const getPostInfo = async () => {
const {data} = await axios.get('https://www.imdb.com/search/title/?groups=top_1000&ref_=adv_prv');
// console.log(data)
const $ = cheerio.load(data);
const titles = [];
const date = [];
const runtime = [];
const rating = [];
const metascore = [];
const votes = [];
const grossEarning = [];
$('h3 a').each((i, el) => {
titles[i] = $(el).text().trim();
})
$('h3 .lister-item-year').each((i, el) => {
date[i] = $(el).text();
})
$('.runtime').each((i, el) => {
runtime[i] = $(el).text().trim();
});
$('.ratings-imdb-rating').each((i, el) => {
rating[i] = $(el).text().trim();
})
$('.ratings-bar').each((i, el) => {
if ($(el).find('.ratings-metascore .favorable').length > 0) {
metascore[i] = $(el).find('.ratings-metascore .favorable').text().trim();
}
if ($(el).find('.ratings-metascore .mixed').length > 0) {
metascore[i] = $(el).find('.ratings-metascore .mixed').text().trim();
}
})
const nv = [];
$('.sort-num_votes-visible').each((i, el) => {
if($(el).find('span')){
// text-muted has text 'votes:', however we need the number of votes which is in next() span tag
nv[i] = $(el).find('.text-muted').next().text();
votes[i] = nv[i].split('$')[0];
grossEarning[i] = '$' + nv[i].split('$')[1];
}
})
for (let i = 0; i < 50; i++) {
object[i] = {
title: titles[i],
date: date[i],
runtime: runtime[i],
rating: rating[i],
metascore: metascore[i],
votes: votes[i],
grossEarning: grossEarning[i]
};
}
// This does not work but console.log(object) gives me a list of objects
return object
// console.log(object);
}
getPostInfo()

How can I auto increment a value whenever a member is mentioned in a message

client.on('message', async function(message) {
if(message.author.bot) return;
if(message.content.includes('!reps')) {
let usname = message.mentions.users.first().username;
let usId = message.mentions.users.first().id;
message.channel.send('Calculating ' + usname + `'s reps`);
let repsChannel = client.channels.cache.get('66518878191720806');
let sbChannel = client.channels.cache.get('66503449499402200');
let chData = await repsChannel.messages.fetch( {limit:100}).catch(err => console.log(err));
let mentamt = chData.filter(chData => chData.mentions.members.has(usId))
let amts = mentamt.size
console.log(amts)
This portion gives me the first 100 lines to check through and it seems to work, however, after including the While loop to check through the next 100 lines, I cant make it return the total sum
while(chData.size === 100) {
let lastKeyId = chData.lastKey();
chData = await repsChannel.messages.fetch( {limit:100, before: lastKeyId }).catch(err => console.log(err));
let tvalue = chData.filter(chData => chData.mentions.members.has(usId)
let tamt = 0
}
sbChannel.send('> '+ usname +' has '+ amts);
}
});
I want to auto increment the value of tamt whenever a member has been mentioned in a message.
The main purpose of this is to calculate the total number of times a member has been mentioned.
Using async-await in for loop, you can achieve it easily.
const delay = () =>
new Promise((r) => {
setTimeout(r, 300, new Date());
});
(async function () {
async function page(count) {
const result = await delay();
// Do your work here
return `Page ${count}:: ${result}`;
}
const chunkSize = 100;
const pageCount = Math.floor(1121/chunkSize) + 1
for (let pageNum = 0; pageNum < pageCount; pageNum++) {
const data = await page(pageNum);
console.log(data)
}
})();

Categories

Resources