I made a nodeJS script that has a parameter.
At this state, i can pass arguments to npm start and it will fire up my script with it, and all works perfectly.
Now I have to pass multiple arguments to npm start and it will fire up as many scripts needed. ( one per argument )
But I really have no idea how to do that except to make the other script that accepts all arguments and launch etc... but I don't want this solution. So if some of you have the answer it will be nice!
Thanks.
Given that you're fine with the arguments being handled in a single process, the answer is simple. Whatever way your current program runs on a single argument, make that into a function and pass into it your multiple arguments, separately.
According to this answer, you can access the arguments array via process.argv.
So if your program used to do console.log(process.argv[0]), what you would do now is make your previous program into a function, function run(arg) { console.log(arg) }, and then run that function on each argument, process.argv.forEach(run)
Related
In my Python program, I need to run the hcaptchaCallback function (screenshot), but the function name has a different numbers each time. Can I somehow run this function by specifying only the first part of the name (hcaptchaCallback)?
driver.execute_script('hcaptchaCallback...()')
There is nothing that lets you really do that. You might be able to pull off looping over the globals and looking for a variable that starts with your string.
window[Object.keys(window).find(k=>k.startsWith("hcaptchaCallback"))]();
that will call it the way you showed, but looking at the function signature, looks like you are going to have to use call()
In my express.js project everything works fine, but having several routing files an other function files, I feel a bit like drowning in all the functions, not knowing which functions run, when, and by which caller.
For example, I have two functions that may run too many times in event loop: the router.use which I see as a constructor to all related routes (from which I want to filter out some), and verify token function that should run only when certain conditions are met, such as API calls, or regular calls that detect admin cookie etc.
I'm trying to generate a log that can help me find which functions are called more than once and by which caller.
Sort of like a map per event.
I use console.log to see order and current call url when firing an event, and I also use node-inspector's breakpoints to see how many times functions like router.use are called, but when manually following the functions in node-inspector, many node core files are involved in the process, which makes the task more tedious because I am interested to debug only my functions... and it doesn't let me see the bigger picture, more readable information on a zoomed out perspective.
What is the preferable method to generate such a log/report?
I need something to work like a callback for a Meteor helper, such that it runs every time the helper updates/returns. I can't include it in the helper definition because a) it would run before the helper returns, and b) as far as I can tell that code only runs once. Similarly, the Template.foo.rendered callback seems to also only run once (not when the helper updates), and not even after the helper returns the first time. So, is there any way to execute code after a Meteor helper returns? The only thing I can think of right now is a timer, and that seems quite messy and wrong. Thanks!
If the updated data is coming from a database, would using a observeChanges work?
To me, the following code seems reasonable enough:
$("#oneButton").click(
alert("hello");
);
It seems to say that when OneButton is clicked, please pop up an alert saying "hello".
However, in reality, the alert pops up regardless of whether the button is clicked or not.
One has to wrap alert("hello"); in an anonymous function, and THEN (and only then), the alert popping up will depend on clicking the button. To me this seems unnecessarily convoluted.
There must be a good reason why the designers of jQuery thought it acceptable for the alert in the code above to pop up even when the button hasn't been clicked. What is this reason?
Fair question I guess, although I'm not a fan of the arrogance that came with it :)
Lets break this down a bit:
object.method(function() {
alert('hi');
});
Your question is, why can't I skip the anonymous function?
What we're really doing here, is telling method to execute something at a later point. What's being executed is being supplied as a function.
We could simply give it a reference to a function instead!
object.method(alert);
Here's the problem, we've sent it a function, but now we can't send it any arguments. If we want to bring arument along to method, we must use ( and ).
As soon as those characters are included, the alert will instead get executed and the result of alert will get sent to method.
Now why can't this also be sent by reference? A very simple reason, you need some way to also pass the result of a function as an argument to another function, and the javascript engine cannot distinguish if your intent is to:
Send the result of a function as an argument to this other function, OR
Send a reference to the function with certain arguments to the other function.
Using ( and ) on a function means calling it immediately in almost every programming language, and javascript is no different.
There is a workaround:
object.method(alert.bind(this, "hi"));
Because .click() is a function and it may need a/some parameter/s to be used properly.
.click() alone will only trigger the event.
But .click(parameter) will do what's in the parameter after the event has been triggered. In this case parameter is a callback, i.e a function called after the main function finished.
But for the callback to be called, you will have to create a function.
Either by naming one:
function alertThis(){
alert('hello');
}
$("#oneButton").click(alertThis);
Or:
$("#oneButton").click(function(){
alert('hello');
});
The language could only provide a less verbose syntax for functions (look at coffeescript), but it's ok for library functions to execute immediately, so you can wrap them and pass code around when needed.
What do you suggest instead? I can only think of one alternative approach where primitive API return themselves a function, but that would lead to uglier code when you need to combine multiple primitives (even with direct syntax support by the language):
$(btn).onClick(alert().andThen(blink()).andThen(log()));
And also you would be forced to call
alert()()
When you need to display the dialog immediately.
The problem is that Javascript has no macros (with the meaning of Lisp) and the only way to provide "code" to a function is by passing a function/closure object.
click is just a regular method and accepts a parameter that is "code" to execute when the button is clicked. As for any parameter however the expression passed to click is evaluated when making the call and not later when user clicks the button.
To have click to work as you like the syntax should handle click differently than other function calls and this is what is allowed for example in Lisp by using macros instead of functions. Javascript has no macros and the syntax is fixed in the language: it doesn't have a click special form and you cannot create one.
The situation is not that terrible because Javascript syntax allows for inline anonymous functions so basically you just need to wrap your "code" parameters with function(){...} at the call site to get it working.
In Python for example things are a bit more annoying because only extremely simple functions can be specified inline as the lambda form can be used to specify anonymous inline functions but has serious limitations and doesn't allow any statement but just a single expression.
Anyone can tell me how to activate two (or more) JavaScript AJAX functions in parallel?
This is not possible. Javascript can only work in a single thread and there is no way to actually have two functions running in parallel. You need to make one call and then the other. The callbacks of these will be called (not necessarily in the same order with the invocation methods), when data have been returned or an error/timeout occurs. Only when one callback completes, will the second one be allowed to run.
Have also in mind that browsers restrict the number of active ajax calls. So, if you try to make too many ajax calls, one might wait (blocking all javascript code) for other calls to complete.
Search for Web Workers. These are kind of a new feature in modern browsers and may not be available in old ones.
Is this what you're looking for?
setTimeout('JsFunction1(val);', 0);
setTimeout('JsFunction2(val);', 0);
use Web Workers to run tasks in Parallel
You can a tutorial here: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers
Also, this library, which takes advantage of web workers, came up pretty fast on google: https://parallel.js.org/
Using several 'setInterval' may make parallel running possible, though it may still run on a single core. The following code is an example of parallelizing a function func with an array of data 'datas'. To run it, use Parallel(func,datas) where func is the name of your global function and datas is an array of data each one as an input for func.
var i_array=new Array();
function Parallel(func,datas){
$(datas).each(function(i,v){
i_array[i]=setInterval(function(){
clearInterval(i_array[i]);
window[func](datas[i]);
},10);
});
}
Here is a jsfiddle test. The time stamp in integer numbers show the two ajax were running in parallel.
Use window.open() to call a new page. That page will call the first js function. After window.open() calls the second function, you are not technically waiting for the first function to complete. You just wait for the window.open() to execute and then the second will get execute.
Javascript runs as a single thread, if the requests that you want to make doesn't have an IO, In that case its just not possible, if there's an IO operation involved, you can very well execute the two functions one after the other, the very nature of javascript will start executing the next function when it waits for IO.
Usually in languages that support threading the same thing is achieved automatically during the CPU rest period for a thread.