Lessons learned from Node.js - Callbacks / Promises

Post on 06-May-2015

397 views 5 download

description

Lessons learned from node.js. Node.js methodology, working with callbacks, using promises. If you ever decide to one day sit down and write in node.js just remember to use promises. Instead of calling a passed callback, return a promise.

Transcript of Lessons learned from Node.js - Callbacks / Promises

Lessons Learned from Node.js

Jason YauDirector of EngineeringSamsung Accelerator

Tuesday, October 8, 13

1. Node.js

2. Methodology

3. Callbacks

4. Promises

Tuesday, October 8, 13

Node.js is javascript on the server side.

It’s event-driven, non-blocking I/O model makes it FAST and

scalable. Tuesday, October 8, 13

Tuesday, October 8, 13

In most languages, the code we write is

synchronous.

aka Blocking

Tuesday, October 8, 13

Blocking

Tuesday, October 8, 13

Waiting

Tuesday, October 8, 13

In javascript, we do things differently; asynchronously.

aka Non-blocking

Tuesday, October 8, 13

Non-Blocking

Tuesday, October 8, 13

Parallel

Tuesday, October 8, 13

Fast

http://benchmarksgame.alioth.debian.org/u64/benchmark.php?test=all&lang=v8&lang2=yarv&data=u64

Tuesday, October 8, 13

Javascript

Tuesday, October 8, 13

Popular

Tuesday, October 8, 13

Methodology

Tuesday, October 8, 13

I/O Latency• L1: 3 cycles

• L2: 14 cycles

• RAM: 250 cycles

• DISK: 41,000,000 cycles

• NETWORK: 240,000,000 cycles

Tuesday, October 8, 13

A theoretical request.Request Process Results

Query DB or Web Service Write to log file

Format Response

= blocking / waitingTuesday, October 8, 13

Scale with Threads

Scale with Processes

Thread 1Thread 2Thread 3Thread 4

Process 1Process 2Process 3Process 4

Tuesday, October 8, 13

Scale with Event LoopHandles many concurrent requests in a single

process / thread

Process 1

Thread Pool

Tuesday, October 8, 13

Analogy

Tuesday, October 8, 13

Event Queue Thread Pool

Event Loop

FilesystemNetworkProcessOther

Scale with Event Loop

Tuesday, October 8, 13

Challenges with Callbacks

Tuesday, October 8, 13

Recurring Question

Tuesday, October 8, 13

Callbacks Example

Tuesday, October 8, 13

Pyramid of Doom

Tuesday, October 8, 13

Debugging

Tuesday, October 8, 13

Parallel Example

Tuesday, October 8, 13

Promises

Tuesday, October 8, 13

What are promises?

A promise is an object that represents a one-time event, typically the outcome of an async task like an AJAX call. When a promise is returned, at first it is in a pending state.

Eventually it’s either:1. resolved - The promise is fulfilled with a value2. rejected - The promise is broken

Tuesday, October 8, 13

Instead of calling a passed callback, return a promise.

Tuesday, October 8, 13

Callback example

Tuesday, October 8, 13

Return a promise

Tuesday, October 8, 13

Passed callback

Tuesday, October 8, 13

Return a promise

Tuesday, October 8, 13

Parallel I/O

Tuesday, October 8, 13

Promises are cleaner

Tuesday, October 8, 13

Instead of calling a passed callback, return a promise.

Promise me you’ll remember promises.

Tuesday, October 8, 13

Thank YouJason Yau

Director of Engineering, Samsung work: jason@samsungaccelerator.com

Tuesday, October 8, 13