Hey There i came across an issue recently while performing the Async db calls inside for loops in javascript.
ex:
for (let i=0 ; i<10 ; i++) {
db.User.find({},function(err,res){
console.log(i);
});
}
Consider above code snippet. Here if you see db is a database call and it takes obviously time to execute query and hit call back but our for loop wont wait till it hit the call back. The for loop will be executed even before you receive the result from call back.
So you will not get actual result .
How I solved it
I used promises to solve it
q is a package from NOde js and in angular js (Now Angular 2 moved to Observables)
Steps:
1. Frame all the async function calls you want to make in for loop.
2. Pass all this async calls in Q.all() function. (Q.all will get all the promise objects from the async functions you pass and club in the form of array.)
3. Now using this array you can map and reduce the results.
Ex:
asyncFunc1()
{
setTimeout(function() {
return 'Hello' }, 3000); // consider this returns after 3 s of execution
}
asyncFunc2()
{
setTimeout(function() {
return 'Hello' }, 5000); // consider this returns after 5 s of execution
}
asyncFunc3()
{
setTimeout(function() {
return 'Hello' }, 2000); // consider this returns after 2 s of execution
}
var asyncCalls = [];
asyncCalls[0] = asyncFunc1();
asyncCalls[1] = asyncFunc2();
asyncCalls[2] = asyncFunc3();
now pass this array of function calls to Q.all like below
var Q = require('q');
Q.all(asynCalls,function(res) {
console.log(res) // this result is nothing but array of collecting objects from asynccalls. now you can
//map and reduce the results according to you .
})
NS2 downloads,Angular js ,NS2 TCL Scripts,NS2 Install,NS2 Awk,Xgraph,NS2 research,NS2 problems,NS2 trace files, NS2 help,NS2 solutions,NS2 routing protocols download,MAODV download,PUMA download,AODV awk scripts,AODV throughput download,awk script for AODV,energy for AODV,end-toend delay for NS2,jitter for NS2,
Tuesday, 16 May 2017
Wednesday, 3 May 2017
How to Copy files from MAC to pen-drive- NTFS
Install NTFS-3G from Homebrew by opening a Terminal and entering the following command.
brew install ntfs-3g
After installing NTFS-3G you can manually mount NTFS volumes in read-write mode by executing the following commands in Terminal. Replace
/dev/disk1s1 with the actual NTFS partition you want to mount. You can find the partition name using diskutil list.sudo mkdir /Volumes/NTFS
sudo /usr/local/bin/ntfs-3g /dev/disk1s1 /Volumes/NTFS -olocal -oallow_other
if you face issue with mounting . please unmount the disk .
sudo umount /dev/disk2s1
then do the mount.
Labels:
copy mac files to pen drive,
diskutil list,
mac file copy,
mac to windows pen drive copy,
mount,
NTFS,
NTFS file transfer,
umount
Wednesday, 9 November 2016
How to Start Angular Forever as Background process
Hey There,
Angular js process can be started forever using npm package named Forever
Forever is used to start Angular js script npm start in the background process.
Why we need forever package?
Whenever we deploy angular js code in linux environments and do npm start . it will bootstrap the angular js and works fine but when we disconnect the remote connection to linux it will terminate the process automatically.
To avoid such termination while we connected to remote linux environment . we need to start the angular js forever. Forever npm package helps to do this and start the app in the background.
Installation of Forever package globally in linux
[sudo] npm install forever -g
Starting the App with Forever
To start the app, we need to specify the path where npm start script is present. Usually it present in package,json . Hence we need to specify that path.
forever start -c "npm start" /path/to/app/dir/
IF you wish to start from current directory of package.json. Use below command.
forever start -c "npm start" ./
Hope This help you folks. Any doubts please comment.
Subscribe to:
Posts (Atom)