<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=WebFund_2024F_Lecture_15</id>
	<title>WebFund 2024F Lecture 15 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=WebFund_2024F_Lecture_15"/>
	<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2024F_Lecture_15&amp;action=history"/>
	<updated>2026-05-12T21:38:51Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2024F_Lecture_15&amp;diff=24827&amp;oldid=prev</id>
		<title>Soma: Created page with &quot;==Video==  Video from the lecture for November 7, 2024 is now available: * [https://homeostasis.scs.carleton.ca/~soma/webfund-2024f/lectures/comp2406-2024f-lec15-20241107.m4v video] * [https://homeostasis.scs.carleton.ca/~soma/webfund-2024f/lectures/comp2406-2024f-lec15-20241107.cc.vtt auto-generated captions]  ==Notes==  &lt;pre&gt; Lecture 15 ---------- - I am behind on Teams messages &amp; email   - should be caught up by Saturday afternoon - Assignment 3 coming out in the next...&quot;</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2024F_Lecture_15&amp;diff=24827&amp;oldid=prev"/>
		<updated>2024-11-07T19:49:03Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;==Video==  Video from the lecture for November 7, 2024 is now available: * [https://homeostasis.scs.carleton.ca/~soma/webfund-2024f/lectures/comp2406-2024f-lec15-20241107.m4v video] * [https://homeostasis.scs.carleton.ca/~soma/webfund-2024f/lectures/comp2406-2024f-lec15-20241107.cc.vtt auto-generated captions]  ==Notes==  &amp;lt;pre&amp;gt; Lecture 15 ---------- - I am behind on Teams messages &amp;amp; email   - should be caught up by Saturday afternoon - Assignment 3 coming out in the next...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
Video from the lecture for November 7, 2024 is now available:&lt;br /&gt;
* [https://homeostasis.scs.carleton.ca/~soma/webfund-2024f/lectures/comp2406-2024f-lec15-20241107.m4v video]&lt;br /&gt;
* [https://homeostasis.scs.carleton.ca/~soma/webfund-2024f/lectures/comp2406-2024f-lec15-20241107.cc.vtt auto-generated captions]&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 15&lt;br /&gt;
----------&lt;br /&gt;
- I am behind on Teams messages &amp;amp; email&lt;br /&gt;
  - should be caught up by Saturday afternoon&lt;br /&gt;
- Assignment 3 coming out in the next day&lt;br /&gt;
  - based on Tutorial 7 code&lt;br /&gt;
- note all current tutorials are due next week&lt;br /&gt;
  - please get caught up!&lt;br /&gt;
&lt;br /&gt;
Why can&amp;#039;t you call await inside of a regular function?&lt;br /&gt;
 - regular functions must complete executing, they cannot pause&lt;br /&gt;
    - async functions can be paused, with them resuming execution at some arbitrary later time&lt;br /&gt;
 - callers of regular functions expect them to complete!&lt;br /&gt;
 - so if you want to do something async from a synchronous function,&lt;br /&gt;
   you have to use a callback function that will be run once the async&lt;br /&gt;
   function completes&lt;br /&gt;
&lt;br /&gt;
the then method is used to resolve returned promises by registering a callback function. The callback function will be called when the promise is fulfilled.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
So if you are calling a function or method that is asynchronous (and thus returns a promise always), you can either&lt;br /&gt;
 * use await if it is being called inside of an async function&lt;br /&gt;
 * use then if it is being called inside of a synchronous function&lt;br /&gt;
&lt;br /&gt;
So why is req.json() asynchronous?&lt;br /&gt;
 - we have the request, isn&amp;#039;t the body just part of that object?&lt;br /&gt;
 - NO, it is potentially still being sent by the client (browser)&lt;br /&gt;
    - it could be a big file being uploaded!&lt;br /&gt;
 - so the request object can be used to get the body, but it isn&amp;#039;t there yet&lt;br /&gt;
    - we use methods like .formData() and .json() to get the body data and process it&lt;br /&gt;
 - thus, when you&amp;#039;re debugging and you look at the request, you won&amp;#039;t see&lt;br /&gt;
   the request body there - you have to ask for it explicitly instead&lt;br /&gt;
&lt;br /&gt;
Can you use .then on a promise in an async function?&lt;br /&gt;
 - yes, but it makes your code unnecessarily complex most of the time&lt;br /&gt;
 - instead, just use await and then your code reads sequentially&lt;br /&gt;
   rather than having a callback function stuck in the middle of it&lt;br /&gt;
&lt;br /&gt;
JavaScript code, particularly on the server, often got stuck in &amp;quot;callback hell&amp;quot; before the introduction of async/await&lt;br /&gt;
 - callbacks nested inside callbacks nested inside callbacks&lt;br /&gt;
 - became very hard to follow the flow of control of the code&lt;br /&gt;
&lt;br /&gt;
The .then method makes callbacks cleaner when used with promises, but still not as good as async/await.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Remember that web servers are highly concurrent&lt;br /&gt;
 - you have many clients connecting &amp;quot;at once&amp;quot; (multiple requests can be pending)&lt;br /&gt;
 - calls to the database can take arbitrary time&lt;br /&gt;
 - calls to the filesystem can take arbitrary time&lt;br /&gt;
&lt;br /&gt;
So when this happens you have two choices&lt;br /&gt;
 - synchronous: your code pauses until the requested operation finishes, doesn&amp;#039;t do anything else&lt;br /&gt;
 - asynchronous: your code pauses the particular code waiting for something but other parts of the app can keep running&lt;br /&gt;
&lt;br /&gt;
Deno implements what is known as &amp;quot;cooperative multitasking&amp;quot;&lt;br /&gt;
 - different execution contexts pause their work when they are waiting for data but don&amp;#039;t block the execution of other code in the app&lt;br /&gt;
&lt;br /&gt;
Compare this to code in standard Java, Python, or C&lt;br /&gt;
 - when you open or read a file, or access the network,&lt;br /&gt;
   those calls are blocking, meaning&lt;br /&gt;
    - the code executes sequentially&lt;br /&gt;
    - when making a call that may not complete immediately, the&lt;br /&gt;
      rest of the app pauses until that action finishes, e.g.&lt;br /&gt;
      a read() from a file pauses execution until the read finishes,&lt;br /&gt;
      no other code runs&lt;br /&gt;
&lt;br /&gt;
What if you want your code to do something useful while operations are pending, in standard Java, Python, or C?&lt;br /&gt;
 - you use threads or processes&lt;br /&gt;
   - i.e., you logically have multiple copies of the program running at the same time&lt;br /&gt;
   - each one runs independently&lt;br /&gt;
   - with threads, they share memory and so can coordinate that way&lt;br /&gt;
   - with processes, they each have exclusive copies of memory, and so&lt;br /&gt;
     coordination has to happen in some other way (sockets, files)&lt;br /&gt;
&lt;br /&gt;
with pthread library in C, threads can be implemented in multiple ways&lt;br /&gt;
 - kernel &amp;amp; userspace threads, or a mix of the two&lt;br /&gt;
 - beyond the scope of this class, but pretty darn complex&lt;br /&gt;
&lt;br /&gt;
But what we&amp;#039;ve found is that&lt;br /&gt;
 1) processes are heavyweight and make coordination hard&lt;br /&gt;
 2) threads are a pain because interrupting execution means that&lt;br /&gt;
    threads interleave in arbitrary ways, meaning we need locks&lt;br /&gt;
    on data structures to prevent corruption&lt;br /&gt;
 3) cooperative multitasking is more efficient and easier to program,&lt;br /&gt;
    when done with the right abstractions&lt;br /&gt;
&lt;br /&gt;
Deno implements cooperative multitasking with callback functions, promises (and the syntactic sugar of async/await)&lt;br /&gt;
&lt;br /&gt;
This is a complex topic, covered more in operating systems.&lt;br /&gt;
&lt;br /&gt;
But just know&lt;br /&gt;
 - in the first web servers (and many other UNIX servers), every incoming request caused a new process to be created, with that new child process handling the incoming request while the parent waited for new incoming connections&lt;br /&gt;
 - web servers on windows couldn&amp;#039;t do this because process creation was too expensive, so they made a new thread for each incoming request. UNIX servers adopted the same architecture to keep up in performance&lt;br /&gt;
 - later it was realized threads were too expensive to create so often,&lt;br /&gt;
   so they made &amp;quot;thread pools&amp;quot; of already existing threads that would&lt;br /&gt;
   take care of a request and then go back to the pool to wait for new ones&lt;br /&gt;
 - node.js demonstrated that cooperative multitasking could be used to make&lt;br /&gt;
   simpler and extremely fast web servers. Deno followed the lead&lt;br /&gt;
     - was possible because node developed a new async-first approach&lt;br /&gt;
       to I/O&lt;br /&gt;
&lt;br /&gt;
Oddly enough, JavaScript was the perfect language for making a cooperative multitasking web server because it originated in the web browser, and so it had no standard facilities for dealing with files or regular network I/O.&lt;br /&gt;
&lt;br /&gt;
- today you can do async I/O (facilitating cooperative multitasking) in Java, Python, and C/C++; however, it is not the default and you end up mixing sync and async code, and that can cause problems without considerable care&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
State and http&lt;br /&gt;
 - In general, APIs can be stateful or stateless&lt;br /&gt;
 - stateful APIs generally allow for smaller requests and can improve performance through tricks like caching&lt;br /&gt;
 - stateless APIs however can allow for greater parallelism&lt;br /&gt;
   because any necessary context is included with the request&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Classic UNIX file API is stateful&lt;br /&gt;
 - you have to open a file, get a file descriptor&lt;br /&gt;
 - that file descriptor has to be used for subsequent operations&lt;br /&gt;
 - and that file descriptor should be released when no longer needed&lt;br /&gt;
   (i.e., closed), although that happens automatically when a process terminates&lt;br /&gt;
&lt;br /&gt;
Why is it stateful?&lt;br /&gt;
 - because there&amp;#039;s a good bit of work required when finding the data&lt;br /&gt;
   associated with a filename&lt;br /&gt;
 - using a file descriptor allows this work to amoritized across multiple&lt;br /&gt;
   operations (multiple read&amp;#039;s and write&amp;#039;s)&lt;br /&gt;
 - but then we need to tell the OS that we no longer need to access the file&lt;br /&gt;
   so it can release the associated resources, that&amp;#039;s why files should be closed&lt;br /&gt;
&lt;br /&gt;
http, however, is a stateless protocol&lt;br /&gt;
 - every request is independent, GET /list can be understood&lt;br /&gt;
   without reference to any other http request&lt;br /&gt;
 - this has been very useful for scaling the web&lt;br /&gt;
    - because you can do things like replicate web servers&lt;br /&gt;
      and any server can handle any incoming request&lt;br /&gt;
&lt;br /&gt;
HOWEVER, in general we need state in a web app (as opposed to just serving static web documents). So what do we do?&lt;br /&gt;
 - this is where cookies come in&lt;br /&gt;
 - not the only solution, but a very common one&lt;br /&gt;
&lt;br /&gt;
We need some way to tie groups of requests together, indicate they are associated with the same browser/same user&lt;br /&gt;
&lt;br /&gt;
Basic idea&lt;br /&gt;
 - server sends some data to the client on first connection&lt;br /&gt;
 - on later connections, the client sends this data back to the server&lt;br /&gt;
   so the server knows who it is talking to&lt;br /&gt;
&lt;br /&gt;
It is like a &amp;quot;ticket&amp;quot; that is given out and then later can be used to show that you&amp;#039;ve paid for a service.&lt;br /&gt;
&lt;br /&gt;
Whevever you &amp;quot;log in&amp;quot; to a website, there is state, and that state is maintained using things like cookies.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Soma</name></author>
	</entry>
</feed>