Understand JavaScript Callback Functions and Use Them

http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/

A callback function, also known as a higher-order function, is a function that is passed to another function (let’s call this other function “otherFunction”) as a parameter, and the callback function is called (or executed) inside the otherFunction.

Advertisements

What is non-blocking or asynchronous I/O in Node.js?

http://stackoverflow.com/questions/10570246/what-is-non-blocking-or-asynchronous-i-o-in-node-js

Synchronous vs Asynchronous

Synchronous execution usually refers to code executing in sequence. Asynchronous execution refers to execution that doesn’t run in the sequence it appears in the code. In the following example, the synchronous operation causes the alerts to fire in sequence. In the async operation, while alert(2)appears to execute second, it doesn’t.

// Synchronous: 1,2,3
alert(1);
alert(2);
alert(3);

// Asynchronous: 1,3,2
alert(1);
setTimeout(() => alert(2), 0);
alert(3);

Blocking vs Non-blocking

Blocking refers to operations that block further execution until that operation finishes. Non-blocking refers to code that doesn’t block execution. In the given example, localStorage is a blocking operation as it stalls execution to read. On the other hand, fetch is a non-blocking operation as it does not stall alert(3) from execution.

// Blocking: 1,... 2
alert(1);
var value = localStorage.getItem('foo');
alert(2);

// Non-blocking: 1, 3,... 2
alert(1);
$.fetch('example.com').then(() => alert(2));
alert(3);

Advantages

One advantage of non-blocking operations is that the CPU can be kept busy and can save you memory.

Blocking example

An example of blocking is how some web servers like ones in Java or PHP handle requests. If your code does something blocking, like reading something from the database, your code “stalls” at that line and waits for the operation to finish. In that period, your machine is holding onto memory and processing time for a thread that isn’t doing anything.

In order to cater other requests while that thread has stalled depends on your setup. Your server can spawn more threads to cater the request or, if you have a load balancing setup, forwards requests to the next available instance. This instills more setup, more memory consumed, more processing.

Non-blocking example

In contrast, non-blocking servers like ones ones made in Node.JS, only use one thread to service all requests. This might sound counter-intuitive, but the creators designed it with the idea that the I/O is the bottleneck i.e. not computations. This also means an instance of Node makes the most out of a single core. Nothing stops you from spawning another instance to take up another core if you want to get the most out of your hardware as well.

When requests arrive at the server, they are serviced one at a time. When the code serviced needs to query the DB for example, it sends off a request to the DB. However, instead of waiting for the response and stall, it sends the callback to a second queue and the code continues running. Now when the DB returns data, the callback gets queued in a third queue where they are pending execution. When the engine is doing nothing (stack empty), it picks up a callback from the third queue and executes it.

shareedit

Jquery Ajax, return success/error from mvc.net controller

http://stackoverflow.com/questions/26605065/jquery-ajax-return-success-error-from-mvc-net-controller

$.ajax({
                type: "POST",
                data: formData,
                url: "/Forms/GetJobData",
                dataType: 'json',
                contentType: false,
                processData: false,               
                success: function (response) {
                    if (response != null && response.success) {
                        alert(response);
                    } else {
                        alert(response);
                    }                          
                },
                error: function (response) {
                    alert("error!");  // 
                }
            });

Controller:

[HttpPost]
            public ActionResult GetJobData(Jobs jobData)
            {

              var mimeType = jobData.File.ContentType;
              var isFileSupported = AllowedMimeTypes(mimeType);

             if (!isFileSupported){        
                     //  Error
                    return Json(new { success = false, responseText = "The attached file is not supported." }, JsonRequestBehavior.AllowGet);
             }
            else
              {
                    //  Success
                    return Json(new { success = true, responseText= "Your message successfuly sent!"}, JsonRequestBehavior.AllowGet);
               }   

            }

How to use skewer-mode?

http://emacs.stackexchange.com/questions/2376/how-to-use-skewer-mode

Instead of running the demo, just perform these minimal steps to ensure that everything is working correctly.

  1. Open a new buffer with the name myskewer.js
  2. Enable JS2-mode (a dependency of skewer)
  3. Enable skewer-mode
  4. M-xrun-skewer (a browser opens, go back to myskewer.js)
  5. Type alert("hello"); and hit C-xC-e at the end of that line
  6. Go back to the browser.

You should see an alert box on the page.

shareimprove this answer

How to cancel a local git commit

http://stackoverflow.com/questions/4850717/how-to-cancel-a-local-git-commit

Just use git reset without the --hard flag:

git reset HEAD~1

PS: On Unix based systems you can use HEAD^ which is equal to HEAD~1. On Windows HEAD^ will not work because ^ signals a line continuation. So your command prompt will just ask you More?.

shareedit

Error: “Calling ‘Read’ when datareader is closed…”

http://iswwwup.com/t/cb38652a5600/error-calling-read-when-datareader-is-closed.html

1 answer

By dotnetomBest answer

You need to call a method like ToList in you data access layer to force the request to a database:

using (var context = new Admin_TestEntities())
{
    var data = context.Leave_GetDetails("Recommended").ToList();
    return View(data);
}

Methods like AsEnumerable() do not make a request to a database. The request is only made when you first access the data (in your case – in a view) or when you explicitly call a method which makes a call to the database (for example ToList()).

You get an error because when you try to access object in your view this is when the call to the database is actually made, but at the same time your object context is already disposed. To fix it you should explicitly force database query using ToList while the context is not yet disposed, that is in your controller method