Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introducing concurrency through callbacks and closures

Similar presentations


Presentation on theme: "Introducing concurrency through callbacks and closures"— Presentation transcript:

1 Introducing concurrency through callbacks and closures
Javascript Callbacks Introducing concurrency through callbacks and closures

2 Introduction Thesis JavaScript Benefits
fundamental aspects of parallelism and concurrency can be introduced using plain JavaScript. JavaScript the world’s most common programming language is executable within any modern web browser provides no language-level concurrency constructs is single-threaded Benefits all students have access to numerous JavaScript interpreters The simplicity of the language allows students to focus on the computational essence of an algorithm rather than wading through the oft-criticized syntactic jungle of languages like Java

3 Example : Ajax Call var globalVariable; $.ajax({
url: " method: 'GET', success: widget => { globalVariable = widget; } }); console.log( globalVariable); // what prints out?

4 Concurrency and Parallelism
The terms concurrent and parallel are largely synonymous in a non-technical context and even computing professionals often use these terms interchangeably Academics distinguish between concurrency and parallelism. Concurrency is defined as the property of a system in which a set of tasks can remain active and make progress at the same time. Parallelism is the state of a program or algorithm in which concurrency is exploited to support simultaneous execution on multiple processing elements.

5 Closures A closure is an inner function that has access to all variables that are in the outer function, even if the lifetime of the inner function extends beyond the outer function.

6 Closures A closure is an inner function that has access to all variables that are in the outer function, even if the lifetime of the inner function extends beyond the outer function.

7 References Not Values Closures store references to the outer function’s variables; they do not store actual values.

8 References Not Values johnLastTag first = 'John'; first = 'John';
prefix = 'Hello'; tagText : function( last ) { … } setPrefix : function( p ) { prefix = p; } first = 'John'; prefix = 'Hello'; johnLastTag

9 References Not Values johnLastTag first = 'John'; first = 'John';
prefix = 'Hello'; tagText : function( 'Adams' ) { … } setPrefix : function( p ) { prefix = p; } first = 'John'; prefix = 'Hello'; johnLastTag

10 References Not Values johnLastTag first = 'John'; first = 'John';
prefix = 'Hello'; tagText : function( last ) { … } setPrefix : function( p = 'Bonjour' ) { prefix = p; } first = 'John'; prefix = 'Bonjour'; johnLastTag

11 References Not Values johnLastTag first = 'John'; first = 'John';
prefix = 'Hello'; tagText : function( 'Calvin' ) { … } setPrefix : function( p ) { prefix = p; } first = 'John'; prefix = 'Bonjour'; johnLastTag

12 Callbacks with example
A callback function Fcb is a function that is passed to another function Fasync such that when Fasync completes its computation, the callback function Fcb is invoked on the result. setTimeout is a function that accepts a callback (of sorts). The callback doesn't accept input The callback is executed after a specified time delay

13 Pseudo concurrency random delay
inner function that prints a message and returns the runtime execute the printResult function after a delay call asyncFunction 10 times

14 Pseudo concurrency callback function random delay
call asyncFunction 10 times execute the printResult function after asyncFunction is done

15 Parallelism Use XMLHttpRequest object to execute AJAX request.

16 Parallelism Joins function getPetitions( pids, cb ) {
var petitions = []; pids.forEach( pid => getPetition( pid, petition => { petitions.push( petition ); if( petitions.length == pids.length ) cb( petitions ); } ) ); }

17 Note on ES7 ES7 supports async functions and await
A function labeled 'async' returns a Promise Code can 'await' promise resolution Fully supported by FireFox, Chrome, Safari. Not supported by Edge


Download ppt "Introducing concurrency through callbacks and closures"

Similar presentations


Ads by Google