• Complain

Buckler Craig - JavaScript

Here you can read online Buckler Craig - JavaScript full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. City: Sebastopol, year: 2018;2017, publisher: SitePoint Pty, Limited, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Buckler Craig JavaScript

JavaScript: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "JavaScript" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

JavaScript: Best Practice; Clean, Maintainable, Performant Code; JavaScript: Best Practice; Notice of Rights; Notice of Liability; Trademark Notice; About SitePoint; Table of Contents; Preface; Who Should Read This Book?; Conventions Used; Code Samples; Tips, Notes, and Warnings; Hey, You!; Ahem, Excuse Me ... ; Make Sure You Always ... ; Watch Out!; Live Code; Github; The Anatomy of a Modern JavaScript Application; James Kolce; A Note About Node.js; JavaScript ES2015+; Declaring variables; Arrow functions; Improved Class syntax; Promises / Async functions; Modules; Code linting; Modular Code.

Buckler Craig: author's other books


Who wrote JavaScript? Find out the surname, the name of the author of the book and a list of all author's works by series.

JavaScript — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "JavaScript" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Chapter 7: Flow Control in Modern JS: Callbacks to Promises to Async/Await
by Craig Buckler

JavaScript is regularly claimed to be asynchronous. What does that mean? How does it affect development? How has the approach changed in recent years?

Consider the following code:

result1 = doSomething1();result2 = doSomething2(result1);

Most languages process each line synchronously. The first line runs and returns a result. The second line runs once the first has finished regardless of how long it takes.

Single-thread Processing

JavaScript runs on a single processing thread. When executing in a browser tab, everything else stops. This is necessary because changes to the page DOM cant occur on parallel threads; it would be dangerous to have one thread redirecting to a different URL while another attempts to append child nodes.

This is rarely evident to the user, because processing occurs quickly in small chunks. For example, JavaScript detects a button click, runs a calculation, and updates the DOM. Once complete, the browser is free to process the next item on the queue.

Other Languages

Other languages such as PHP also use a single thread but may be managed by by a multi-threaded server such as Apache. Two requests to the same PHP page at the same time can initiate two threads running isolated instances of the PHP runtime.

Going Asynchronous with Callbacks

Single threads raise a problem. What happens when JavaScript calls a slow process such as an Ajax request in the browser or a database operation on the server? That operation could take several seconds even minutes. A browser would become locked while it waited for a response. On the server, a Node.js application would not be able to process further user requests.

The solution is asynchronous processing. Rather than wait for completion, a process is told to call another function when the result is ready. This is known as a callback, and its passed as an argument to any asynchronous function. For example:

doSomethingAsync(callback1);console.log('finished');// call when doSomethingAsync completesfunction callback1(error) { if (!error) console.log('doSomethingAsync complete');}

doSomethingAsync() accepts a callback function as a parameter (only a reference to that function is passed so theres little overhead). It doesnt matter how long doSomethingAsync() takes; all we know is that callback1() will be executed at some point in the future. The console will show:

finisheddoSomethingAsync complete
Callback Hell

Often, a callback is only ever called by one asynchronous function. Its therefore possible to use concise, anonymous inline functions:

doSomethingAsync(error => { if (!error) console.log('doSomethingAsync complete');});

A series of two or more asynchronous calls can be completed in series by nesting callback functions. For example:

async1((err, res) => { if (!err) async2(res, (err, res) => { if (!err) async3(res, (err, res) => { console.log('async1, async2, async3 complete.'); }); });});

Unfortunately, this introduces callback hell a notorious concept that even has its own web page! The code is difficult to read, and will become worse when error-handling logic is added.

Callback hell is relatively rare in client-side coding. It can go two or three levels deep if youre making an Ajax call, updating the DOM and waiting for an animation to complete, but it normally remains manageable.

The situation is different on OS or server processes. A Node.js API call could receive file uploads, update multiple database tables, write to logs, and make further API calls before a response can be sent.

Promises

).

To enable Promise-based execution, asynchronous callback-based functions must be changed so they immediately return a Promise object. That object promises to run one of two functions (passed as arguments) at some point in the future:

  • resolve: a callback function run when processing successfully completes, and
  • reject: an optional callback function run when a failure occurs.

In the example below, a database API provides a connect() method which accepts a callback function. The outer asyncDBconnect() function immediately returns a new Promise and runs either resolve() or reject() once a connection is established or fails:

const db = require('database');// connect to databasefunction asyncDBconnect(param) { return new Promise((resolve, reject) => { db.connect(param, (err, connection) => { if (err) reject(err); else resolve(connection); }); });}

Node.js 8.0+ provides a util.promisify() utility to convert a callback-based function into a Promise-based alternative. There are a couple of conditions:

  1. the callback must be passed as the last parameter to an asynchronous function, and
  2. the callback function must expect an error followed by a value parameter.

Example:

// Node.js: promisify fs.readFileconst util = require('util'), fs = require('fs'), readFileAsync = util.promisify(fs.readFile);readFileAsync('file.txt');

Various client-side libraries also provide promisify options, but you can create one yourself in a few lines:

// promisify a callback function passed as the last parameter// the callback function must accept (err, data) parametersfunction promisify(fn) { return function() { return new Promise( (resolve, reject) => fn( ...Array.from(arguments), (err, data) => err ? reject(err) : resolve(data) ) ); }}// examplefunction wait(time, callback) { setTimeout(() => { callback(null, 'done'); }, time);}const asyncWait = promisify(wait);ayscWait(1000);
Asynchronous Chaining

Anything that returns a Promise can start a series of asynchronous function calls defined in .then() methods. Each is passed the result from the previous resolve:

asyncDBconnect('http://localhost:1234') .then(asyncGetSession) // passed result of asyncDBconnect .then(asyncGetUser) // passed result of asyncGetSession .then(asyncLogAccess) // passed result of asyncGetUser .then(result => { // non-asynchronous function console.log('complete'); // (passed result of asyncLogAccess) return result; // (result passed to next .then()) }) .catch(err => { // called on any reject console.log('error', err); });

Synchronous functions can also be executed in .then() blocks. The returned value is passed to the next .then() (if any).

The .catch() method defines a function thats called when any previous reject is fired. At that point, no further .then() methods will be run. You can have multiple .catch() methods throughout the chain to capture different errors.

ES2018 introduces a .finally() method, which runs any final logic regardless of the outcome for example, to clean up, close a database connection etc. Its currently supported in Chrome and Firefox only, but Technical Committee 39 has released a .finally() polyfill.

function doSomething() { doSomething1() .then(doSomething2) .then(doSomething3) .catch(err => { console.log(err); }) .finally(() => { // tidy-up here! });}
Multiple Asynchronous Calls with Promise.all()

Promise .then() methods run asynchronous functions one after the other. If the order doesnt matter for example, initialising unrelated components its faster to launch all asynchronous functions at the same time and finish when the last (slowest) function runs

Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «JavaScript»

Look at similar books to JavaScript. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «JavaScript»

Discussion, reviews of the book JavaScript and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.