forked from muddledsignal/class
-
Notifications
You must be signed in to change notification settings - Fork 0
Asynchronous Functions, Callbacks, File Systems, Jest Mocks & Promises
Matthew McQuain edited this page Nov 20, 2018
·
1 revision
- Means that different parts of our code don't affect the execution of other parts of our code.
- Asynchronous method invocation (aka async pattern) where the call site is not blocked while waiting for the code we called to finish.
- Is a function that operates asynchronously via the event loop, using an implicit promise to return results.
- Will return a promise which will resolve with the value returned by the async function, or rejected with uncaught exceptions thrown from w/in the async function.
- Remember that Ajax requests don't complete synchronously.
- We use callback functions to wait from "now" until "later".
- Async != Parallel. Async is about the gap between "now" and "later", while parallel is about things being able to occur simultaneously.
- Understand that the single-threaded (one-at-a-time) event loop queue that drives all events (async function invocations).
- Input and output provided by wrappers around POSIX functions.
- Called with "require('fs')
- Takes a completion callback as its last argument when run asynchronously. If successful, first argument will be "null" or "undefined".
- Exceptions are given immediately when run synchronously.
- Ideally we will use the asynchronous versions of calls if at all possible as the synchronous versions will block all processes till they complete.
- Inside our function we want our statements to run in a predictable order (above the compiler level).
- Our function acts as a callback because it serves as the target for the event loop to call back into to the program once that item in the queue is processed.
- Callbacks are the most common way that async in JS programs is handled.
- Callbacks express asynchronous flow in a nonlinear, non-sequential way.
- Callbacks suffer from "inversion of control". They give control to another party to invoke the "continuation" of our program, which can be dangerous for security.
- Our brains are basically operating in a single-threaded event loop queue.
- Be wary of chaining functions together or risk "callback hell" or the "pyramid of doom". This is usually assumed to be a nesting problem, but it's not.
- Callbacks allow a balanced, non-blocking flow of asynchronous control across any modules and applications we are using.
- Error-first callbacks work at scale and use a common, reliable protocol.
- Two rules in error-first callbacks; the first argument of the callback is reserved for an error object. If an error occurs, it will be returned by the first "err" argument. The second argument of the callback is reserved for any successful response data. If no error occurred, "err" will be set to null and any successful data will be returned in the second argument.
- One example is the method "fs.readFile()", which takes in a file path to read from and calls our callback when it's finished. File contents should return in the data argument.
- Callbacks are not limited to being used one at a time and instead can be called in parallel, queue, serial, etc.
- Mocking is a way to isolate test subjects by replacing dependencies with objects that we can control and inspect, usually a module that the subject imports.
- Mock Functions goal is to replace something we don't control with something that we do.
- With the Mock Function we can capture calls, set return values, or change implementation.
- Created with "jest.fn()".
- We can pass the mock function directly as an argument to the function that we want to test, allowing us to run our test subject and assert how the mock was called.
- The "inversion of control" (IoC) is a principle in which custom written parts of a program receive the flow of control from some generic framework.
- We wrap up the continuation of our program in a callback function, and send that callback to another party.
- Promises: have future value as well as values now and later. Individual promises behave like a future value and can be thought of as a flow-control mechanism.
- Slower than callbacks, but more trustworthy.
- Promises redirect the organization of callbacks to a trustworthy intermediary sitting between us and another utility.
- Promise chains help express asynchronous flow in a sequential way.