Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're using a callback to return a value which doesn't make a lot of sense because your Project.findById() returns immediately, without waiting for the provided callback to complete.</p> <p>Don't feel bad though, the programming model that nodejs uses is somewhat difficult at first to wrap your head around.</p> <p>In event-driven programming (EDP), we provide callbacks to accomplish results, ignoring their return values since we never know when the callback might actually be called.</p> <p>Here's a quick example.</p> <p>Suppose we want to write the result of an HTTP request into a file.</p> <p>In a procedural (non-EDP) programming environment, we rely on functions that only return values when they have them to return. </p> <p>So we might write something like (pseudo-code):</p> <pre><code> url = 'http://www.example.com' filepath = './example.txt' content = getContentFromURL(url) writeToFile(filepath,content) print "Done!" </code></pre> <p>which assumes that our program will <strong><em>wait</em></strong> until getContentFromURL() has contacted the remote server, made its request, <strong><em>waited</em></strong> for a result and returned that result to the program. </p> <p>The writeToFile() function then asks the operating system to open a local file at some filepath for write, <strong><em>waiting</em></strong> until it is told the open file operation has completed (typically <strong><em>waiting</em></strong> for the disk driver to report that it can carry out such an operation.)</p> <p>writeToFile() then requests that the operating system write the content to the newly opened file, <strong><em>waiting</em></strong> until it is told that the driver the operating system uses to write files tells it it has accomplished this goal, returning the result to the program so it can tell us that the program has completed.</p> <p>The problem that nodejs was created to solve is to better use all the time wasted by all the <strong><em>waiting</em></strong> that occurs above. </p> <p>It does this by using functions (callbacks) which are called when operations like retrieving a result from a remote web request or writing a file to the filesystem complete.</p> <p>To accomplish the same task above in an event-driven programming environment, we need to write the same program as:</p> <pre><code> getContentFromURL(url,onGetContentFromURLComplete) function onGetContentFromURLComplete(content,err){ writeToFile(content,onWriteToFileComplete); } function onWriteToFileComplete(err){ print "Done!"; } </code></pre> <p>where </p> <ul> <li>calling getContentFromURL() only calls the onGetContentFromURLComplete callback once it has the result of the web request and </li> <li>calling writeToFile() only calls its callback to display a success message when it completes writing the content successfully.</li> </ul> <p>The real magic of nodejs is that it can do all sorts of other things during the surprisingly large amount of time procedural functions have to <strong><em>wait</em></strong> for most time-intensive operations (like those concerned with input and output) to complete.</p> <p>(The examples above ignore all errors which is usually considered a <strong><em>bad thing</em></strong>.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload