How can I send many(10) requestify, sync in nodejs? - arrays

I am developing nodejs application
I have array with 10 URL
var urlArray = [{url01},{url02},{url03},......,{url10}];
var arrayLength = 10;
var reqData = {message:'Wel Come'};
for(var i = 0; i < arrayLength ; i ++ ){
requestify.post(urlArray[i],reqData)
.then(function(resonse){
console.log(response);
},function(err){
console.log(err);
});
}
I need to send 10 requestify sync way

You can do it following way.
var i = 0;
var urlArray = [{url01},{url02},{url03},......,{url10}];
var makeCall = function(x){
if( x < urlArray.length ) {
requestify.post(urlArray[i],reqData)
.then(function(resonse){
makeCall(x+1);
},function(err){
console.log(err);
});
}
};
makeCall(0);

Related

Ajax call output and timeout

Ive build an ajax counter which outputs max 6 digits - e.g. "120398" and repeats the call
function updateNumbers(){
$.ajax({
url: totalnumbers.ajaxurl,
data: {
action: 'numberstotal'
},
success: function (response) {
var numbers = response.toString(10).replace(/\D/g, '0').split('').map(Number);
//console.log(numbers);
var newHTML = [];
for (var i = 0; i < 6; i++) {
newHTML.push('<span>' + numbers[i] + '</span>');
}
$('.counter').html(newHTML.join(""));
}
});
setTimeout(updateMinutes, 5000);
}
updateNumbers();
this works so far.
Question 1:
Now when there are only 4 digits (1234) I still need an output of total like this
<span>0</span>
<span>0</span>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
what I get is
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>undefined</span>
<span>undefined</span>
Question 2:
When theres a total of 200000 I want to quit the setTimeout-Function. But I dont know how to get the response out of the ajax call to check before setting the timeout.
Help would be great. It seems like I am almost there :(
After a little thinking I made it happen:
function updateNumbers(){
$.ajax({
url: totalnumbers.ajaxurl,
data: {
action: 'numberstotal'
},
success: function (response) {
if (response < 20000) {
var stringlength = response.length;
var numbers = response.toString(10).replace(/\D/g, '0').split('').map(Number);
var newNumbers = numbers;
for (var i = stringlength; i < 7; i++) {
newNumbers.unshift(0);
}
var totalNumbers = $.map(newNumbers, function(num) {
return('<span>' + num + '</span>');
});
$('.counter').html(totalNumbers.join(""));
setTimeout(updateNumbers, 5000);
}
else {
var numbers = response.toString(10).replace(/\D/g, '0').split('').map(Number);
var totalNumbers = $.map(numbers, function(num) {
return('<span>' + num + '</span>');
});
$('.counter').html(totalNumbers.join(""));
}
}
});
}
updateNumbers();

async function not waiting to move on

I have a react app, in which im hiding and showing buttons instead of moving across pages, before I show the next button I want to make sure the function the button was meant to call has completed. Here is what I have so far:
This is the Async function:
async handleTransferFromEthereum(){
parseAddress(this.state.sc)
this.setState(prevState => ({
isEthTransferVisible: !prevState.isEthTransferVisible,
isGoDeployedVisible: !prevState.isGoDeployedVisible
}));
}
and this would be the function im calling:
import ERC20ABI from './blockchain/ERC20ABI.js';
import ethweb3 from './blockchain/ethweb3.js';
import _ from 'lodash';
var addressesValues = [];
var uniqueAddresses = [];
var values = [];
var count = 0;
var map = {};
var address =[];
var amounts=[];
var choppedAdrresses = [];
export function parseAddress(_smartcontract){
console.log("Scanning blockchain")
var contractObj = new ethweb3.eth.Contract(ERC20ABI,_smartcontract);
contractObj.getPastEvents(
'Transfer' || 'allEvents',
{
fromBlock: 0,
toBlock: 'latest'
},
function(err,res){
for(var i =1; i< res.length; i++){
if (uniqueAddresses.includes(res[i].returnValues.from)===false) {
uniqueAddresses[count] = res[i].returnValues.from;
values[count] = parseInt(0);
map[uniqueAddresses[count]] = values[count];
count+=1
}
if (uniqueAddresses.includes(res[i].returnValues.to)===false){
uniqueAddresses[count] = res[i].returnValues.to;
values[count] = parseInt(0);
map[uniqueAddresses[count]] = values[count];
count+=1
}
}
for(var j = 0; j< res.length; j++){
map[res[j].returnValues.from] -= parseInt(res[j].returnValues.value);
map[res[j].returnValues.to] += parseInt(res[j].returnValues.value);
}
for(var x = 0; x < uniqueAddresses.length; x++){
addressesValues.push([uniqueAddresses[x], parseInt(map[res[x].returnValues.to])])
}
for(var y=0; y < addressesValues.length; y++){
address.push(addressesValues[y][0]);
amounts.push(addressesValues[y][1]);
}
var choppedAdrresses=_.chunk(address, 100);
var choppedValue=_.chunk(amounts, 100);
var tokenSum = amounts.reduce((a, b) => a + b, 0);
sessionStorage.setItem("addresses", JSON.stringify(address))
sessionStorage.setItem("tokenSum", JSON.stringify(tokenSum))
sessionStorage.setItem("choppedAdrresses", JSON.stringify(choppedAdrresses))
sessionStorage.setItem("choppedValue", JSON.stringify(choppedValue))
}
);
}
Any pointers would really help.
You need to wait on a promise, but since the getPastEvents function works in a callback pattern, you could create a custom promise and return it from parseAddress method
export function parseAddress(_smartcontract){
console.log("Scanning blockchain")
return new Promise(function(resolve, reject) {
var contractObj = new ethweb3.eth.Contract(ERC20ABI,_smartcontract);
contractObj.getPastEvents(
'Transfer' || 'allEvents',
{
fromBlock: 0,
toBlock: 'latest'
},
function(err,res){
if (err) {
reject(err);
}
for(var i =1; i< res.length; i++){
if (uniqueAddresses.includes(res[i].returnValues.from)===false) {
uniqueAddresses[count] = res[i].returnValues.from;
values[count] = parseInt(0);
map[uniqueAddresses[count]] = values[count];
count+=1
}
if (uniqueAddresses.includes(res[i].returnValues.to)===false){
uniqueAddresses[count] = res[i].returnValues.to;
values[count] = parseInt(0);
map[uniqueAddresses[count]] = values[count];
count+=1
}
}
for(var j = 0; j< res.length; j++){
map[res[j].returnValues.from] -= parseInt(res[j].returnValues.value);
map[res[j].returnValues.to] += parseInt(res[j].returnValues.value);
}
for(var x = 0; x < uniqueAddresses.length; x++){
addressesValues.push([uniqueAddresses[x], parseInt(map[res[x].returnValues.to])])
}
for(var y=0; y < addressesValues.length; y++){
address.push(addressesValues[y][0]);
amounts.push(addressesValues[y][1]);
}
var choppedAdrresses=_.chunk(address, 100);
var choppedValue=_.chunk(amounts, 100);
var tokenSum = amounts.reduce((a, b) => a + b, 0);
sessionStorage.setItem("addresses", JSON.stringify(address))
sessionStorage.setItem("tokenSum", JSON.stringify(tokenSum))
sessionStorage.setItem("choppedAdrresses", JSON.stringify(choppedAdrresses))
sessionStorage.setItem("choppedValue", JSON.stringify(choppedValue))
resolve();
}
);
});
}
After this, you can use await like
async handleTransferFromEthereum(){
await parseAddress(this.state.sc)
this.setState(prevState => ({
isEthTransferVisible: !prevState.isEthTransferVisible,
isGoDeployedVisible: !prevState.isGoDeployedVisible
}));
}
However if its possible try to convert getPastEvents so that it returns a promise instead of using a callback
The promise starts when you create it and await will wait for it to resolve. But the code in the middle of these two statements will run before the promise ends. If you don't need any result of the promise you can put code in that zone to paralelize the execution of it with the promise.
const timeBomb = (resolve, reject) => {
const msleft = Math.random() * 700 + 200;
console.log("Countdown started!");
console.log("Only "+(Math.round(msleft)/1000)+" seconds left!!");
setTimeout(resolve, msleft);
};
waiter = async (p) => {
console.log("I will hurry to do my last wish before dying");
const result = await p;
console.log("It Exploded!");
}
waiter(new Promise(timeBomb));

Using nested $http.get calls in Angular JS sequentially

I have this situation where two $http.get calls are nested.I get result from the first call and then iterating over this first result and passing this to another $http.get call and in the end I amtrying to make whole thing as ab array of objects.I am finding that ,this whole is not happening in sequence.Could someone help me out?
$scope.populateData = function()
{
$scope.infoWithStatus = [];
$http.get("commonAppGet.jsp?sqlStr=select name from test where title_id=1").then(function(resp){
$scope.names = resp.data.d;
for(var i=0;$scope.names.length;i++){
infoObject= {};
var c1=0;c2=0; c3=0;c4=0;c5=0;
$scope.spocName = $scope.names[i].name;
infoObject.name=$scope.spocName;
$http.get("commonAppGet.jsp?sqlStr=select a.status as status from test1 where name='"+$scope.spocName+"'").then(function(resp){
$scope.statusValues = resp.data.d;
for(var i=0;i<$scope.statusValues.length;i++)
{
if($scope.statusValues[i].status==0)
c1++;
if($scope.statusValues[i].status==1)
c2++;
//some code for c3,c4,c5
}
infoObject.count1=c1;
infoObject.count2=c2;
infoObject.count3=c3;
infoObject.count4=c4;
infoObject.count5=c5;
});
$scope.infoWithStatus.push(infoObject);
}
});
}
Maybe this will be you
I saw that you missing i < $scope.names.length in the first promise
$scope.populateData = function()
{
$scope.infoWithStatus = [];
var c1=0;c2=0; c3=0;c4=0;c5=0;
$http.get("commonAppGet.jsp?sqlStr=select name from test where title_id=1").then(function(resp){
$scope.names = resp.data.d;
var listPromise = [];
for(var i=0;i < $scope.names.length;i++){
infoObject= {};
$scope.spocName = $scope.names[i].name;
infoObject.name=$scope.spocName;
listPromise.push($http.get("commonAppGet.jsp?sqlStr=select a.status as status from test1 where name='"+$scope.spocName+"'"));
$scope.infoWithStatus.push(infoObject);
}
return Promise.all(listPromise);
}).then(function(resp){
for (var i = 0; i < resp.length; i++) {
$scope.statusValues = resp[i].data.d;
for(var i=0;i<$scope.statusValues.length;i++)
{
if($scope.statusValues[i].status==0)
c1++;
if($scope.statusValues[i].status==1)
c2++;
//some code for c3,c4,c5
}
infoObject.count1=c1;
infoObject.count2=c2;
infoObject.count3=c3;
infoObject.count4=c4;
infoObject.count5=c5;
}
});
}

socket-io - matching learning and speaking languages

I'm trying to make a chat app (similar to Omegle.com) that matches native speakers to learners. I believe I have the correct algorithm, but my Javascript keeps throwing a message that Javascript heap out of memory error.
For example, let's say Speaker 1 speaks English and learns French and Speaker 2 speaks French and learns English (perfect match). My algorithm displays match found.
Queue Controller
socket.emit('in queue', {
speakingLanguages: speakingLanguages,
learningLanguages: learningLanguages
});
socket.on('chat start', function(data){
room = data.room;
$location.path('/chat');
});
server.js
var queue = [];
var rooms = {};
io.sockets.on('connection', function(socket){
console.log('User ' + socket.id + ' connected');
socket.on('in queue', function(data){
socket.speakingLanguages = data.speakingLanguages;
socket.learningLanguages = data.learningLanguages;
if (queue.length != 0){
for (var i = 0; i < queue.length; i++){
for (var j = 0; j < queue[i].speakingLanguages.length; j++){
for (var y = 0; y < socket.learningLanguages.length; y++){
if (queue[i].speakingLanguages[j] === socket.learningLanguages[y]){
console.log('a match was found!');
break;
}else{
queue.push(socket);
break;
}
}
}
}
}else{
queue.push(socket);
}
});
socket.on('send message', function(data){
var room = rooms[socket.id];
io.sockets.in(room).emit('send message', data);
});
});
But see, when I open up multiple windows on my browser to test a lot of cases, I get that error above. It says that I ran out of Javascript heap and I've been struggling with this for hours, but I don't know what to do.
Information:
speakingLanguages and learningLanguages are of type array (because people can speak multiple languages and learn multiple languages)
Every time a match is not found, I put them into the queue array
I was diligently writing down notes and investigating the algorithm and I think I've finally got it. I tested the algorithm and it seems to be working!
socket.on('in queue', function(data){
socket.speakingLanguages = data.speakingLanguages;
socket.learningLanguages = data.learningLanguages;
var found = false;
if (queue.length != 0){
for (var i = 0; i < socket.learningLanguages.length; i++){
for (var j = 0; j < queue.length; j++){
for (var y = 0; y < queue[j].speakingLanguages.length; y++){
if (socket.learningLanguages[i] === queue[j].speakingLanguages[y]){
console.log('a match was found');
var peer = queue.pop();
var room = socket.id + '#' + peer.id;
peer.join(room);
socket.join(room);
rooms[peer.id] = room;
rooms[socket.id] = room;
peer.emit('chat start', { room: room });
socket.emit('chat start', { room : room });
found = true;
break;
}
}
}
}
if (!found){
queue.push(socket);
}
}else{
queue.push(socket);
}
});
There is way too much for loop nesting in your code.
I made some changes to your code.
io.sockets.on('connection', function(socket){
console.log('User ' + socket.id + ' connected');
socket.on('in queue', function(data){
socket.speakingLanguages = data.speakingLanguages;
socket.learningLanguages = data.learningLanguages;
var searchingFor = [];
var toQueue = {};
var toUnqueue = {};
for (let spoken of socket.speakingLanguages) {
for (let learning of socket.learningLanguages) {
var searchKey = `S:${learning}L:${spoken}`;
var queueKey = `S:${spoken}L:${learning}`;
searchingFor.push(searchKey);
toQueue[queueKey] = socket;
toUnqueue[queueKey] = undefined;//Using `undefined` instead of `delete` for performance
}
}
//Search for a peer
var peer = false;//use an array[] if you want all matches
for (let searching of searchingFor) {
let result = queue[searching];
if(result) {
peer = result;
break;//You can change this part to find all matches
}
}
if (!peer) {
//No peer(s) found
//Add all possible combination of `spoken:learning` keys to the queue
//If someone searchs for one of these combinations we will be matched
socket.toUnqueue = toUnqueue;
Object.assign(queue, toQueue);
} else {
//We found a matching peer
console.log('a match was found');//If you use multiple matches you can ask what he prefers
//Unqueue other language combinations of the peer
Obeject.assign(queue, peer.toUnqueue);
//The rest of you logic goes here
var room = socket.id + '#' + peer.id;
peer.join(room);
socket.join(room);
rooms[peer.id] = room;
rooms[socket.id] = room;
peer.emit('chat start', { room: room });
socket.emit('chat start', { room : room });
}
});
socket.on('send message', function(data){
var room = rooms[socket.id];
io.sockets.in(room).emit('send message', data);
});
});
I changed the way you're using the queue.
Searching by key should be faster.
I didn't test it but it should be good.Tell me if it does the job!
I revised my code a bit using amenzou's answer and another one here
var queue = [];
var rooms = {};
var index = 0;
var intersect_safe = function(a, b){
var ai=0, bi=0;
var result = [];
while( ai < a.length && bi < b.length )
{
if (a[ai] < b[bi] ){ ai++; }
else if (a[ai] > b[bi] ){ bi++; }
else /* they're equal */
{
result.push(a[ai]);
ai++;
bi++;
}
}
return result;
}
io.sockets.on('connection', function(socket){
console.log('User ' + socket.id + ' connected');
socket.on('in queue', function(data){
socket.speakingLanguages = data.speakingLanguages;
socket.learningLanguages = data.learningLanguages;
if (queue.length != 0){
for (var i = 0; i < queue.length; i++){
var match = false;
var match1 = [];
var match2 = [];
match1 = intersect_safe(socket.learningLanguages, queue[i].speakingLanguages);
match2 = intersect_safe(socket.speakingLanguages, queue[i].learningLanguages);
if (match1.length != 0 && match2.length != 0){
console.log('match');
index = i;
match = true;
break;
}
}
if (match){
var peer = queue.splice(index, 1);
var room = socket.id + '#' + peer[0].id;
peer[0].join(room);
socket.join(room);
rooms[peer[0].id] = room;
rooms[socket.id] = room;
peer[0].emit('chat start', match2);
socket.emit('chat start', match1);
}else{
queue.push(socket);
}
}else{
queue.push(socket);
}
});
Works perfectly! Thank you very much for your help and advice, amenzou.

nightmare how to looply select the selector

var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });
var fs = require('fs');
var result;
nightmare
.goto('http://football-system.jp/fss/pub_penaltylist.php?lid=eBVesRz5C54=')
.wait('select[name="selectTeam"]')
.evaluate(function () {
var options = document.querySelectorAll('option'),i;
var values =[]
for (i = 1; i < options.length; ++i) {
values.push(options[i].value)
}
return values;
})
.then(function (values) {
console.log(values)
values.reduce(function(accumulator, value) {
return accumulator.then(function(results) {
return nightmare.goto("http://football-system.jp/fss/pub_penaltylist.php?lid=eBVesRz5C54=")
.wait('select[name="selectTeam"]')
.select('select[name="selectTeam"]', value)
.wait('button[onClick="selectData();"]')
.click('button[onClick="selectData();"]')
.wait('table[class="tableOutputDate"]')
.evaluate(function () {
return document.querySelector('table[class="tableOutputDate"]').textContent;
})
.then(function(result){
console.log(result)
results.push(result);
return results;
});
});
}, Promise.resolve([])).then(function(results){
console.log(results)
});
})
.catch(function (error) {
console.error('Search failed:', error);
});
That is my code .I want to looping select all the selector in that page and get the all html data.I asked a question in here that how to loop in nightmare,but that result cant solve this problem.please help me. Thank you.
I solve my question.the problem is happened in the deal time,not in the loop logic.I add some more wait(time).I think wait(time)is better than the wait(selector).so is over. thank u attention.
var run = function * () {
//var values = ['http://www.yahoo.com', 'http://example.com', 'http://w3c.org'];
var titles = [];
for (var i = 0; i < values.length; i++) {
var title = yield nightmare.goto('.............')
//.wait('select[name="selectTeam"]')
.wait(2000)
.select('select[name="selectTeam"]', values[i])
.wait(2000)
.click('button[onClick="selectData();"]')
//.wait('table[class="tableOutputDate"]')
.wait(2000)
.evaluate(function () {
//return document.querySelector('table[class="tableOutputDate"]').textContent;
var divs = document.querySelectorAll('table[class="tableOutputDate"]'),i;
var tables = []
for (i = 0; i < divs.length; ++i) {
tables.push(divs[i].textContent)
//result += divs[i].href.toString()+"\n";
}
return tables;
})
titles.push(title);
}
return titles;
}
vo(run)(function(err, titles) {
console.dir(titles);
});

Resources