Tonight's workshopper...
Access Type | Cycles |
---|---|
L1 Cache | 3 |
L2 Cache | 14 |
RAM | 250 |
Disk | 41 000 000 |
Network | 240 000 000 |
Recap
Source: http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
var fs = require('fs');
fs.readFile(
'file1.txt',
function (err, data) {
if (err) {throw err;}
console.log(data);
}
);
var fs = require('fs');
fs.readFile(
'file1.txt',
function (err, data) {
if (err) {throw err;}
fs.readFile(
'file2.txt',
function (err, data) {
if (err) {throw err;}
console.log(data);
}
);
}
);
var fs = require('fs'),
numCompleted = 0,
onFileRead = function (err, data) {
if (err) {throw err;}
numCompleted += 1;
if (numCompleted === 2) {
// Both async operation have been completed
}
};
fs.readFile('file1.txt', onFileRead);
fs.readFile('file2.txt', onFileRead);
A placeholder for a value that may be available in the future.
It will be fulfilled or rejected... once!
var file1Promise = readFilePromise('file1.txt');
file1Promise.then(
function onFulfilled(data) {console.log(data);},
function onRejected(err) {throw err;}
);
var promise1 = readFilePromise('file1.txt');
var wat = promise1.then(
function onFulfilled(data) { /* ... */ },
function onRejected(err) { /* ... */ }
);
Q: What does then() return?
(What is wat?)
var promise1 = readFilePromise('file1.txt');
var promise2 = promise1.then(
function onFulfilled(data) { /* ... */ },
function onRejected(err) { /* ... */ }
);
Q: What does then() return?
A: A promise
var promise1 = readFilePromise('file1.txt');
var promise2 = promise1.then(
function onFulfilled(data) { /* ... */ },
function onRejected(err) { /* ... */ }
);
1 of 3 things can happen in your handlers:
readFilePromise('file1.txt').then(
function onFulfilled(data) {
console.log(data);
return readFilePromise('file2.txt');
}
).then(
function onFulfilled(data) {console.log(data);}
).catch(
function onRejected(err) {throw err;}
);
var q = require('q'),
aggregatePromise,
thePromises = [readFilePromise('file1.txt'), readFilePromise('file2.txt')];
aggregatePromise = q.all(thePromises);
aggregatePromise.then(
function (values) {
// values[0] is file1.txt data
// values[1] is file2.txt data
}
).catch(function onRejected(err) {throw err;});
(The old way)
var fs = require('fs'),
q = require('q');
function promiseReadFile(fileName) {
var dfd = q.defer();
fs.readFile(fileName, function (err, data) {
if (err) { return dfd.reject(err); }
dfd.resolve(data);
});
return dfd.promise;
}
(The new way)
function promiseReadFile(fileName) {
var promise = new Promise(
function (resolve, reject) {
fs.readFile(
'myfile.txt',
function (err, data) {
if (err) {
return reject(err);
}
resolve(data);
}
);
}
);
return promise;
}
promise.spread()
promise.allSettled()
promise.any()
promise.race()
promise.finally()
To get setup:
$ mkdir promise-it-wont-hurt && cd promise-it-wont-hurt
$ npm install q
$ npm install -g promise-it-wont-hurt@latest
To run:
$ promise-it-wont-hurt
$ promise-it-wont-hurt run program.js
$ promise-it-wont-hurt verfiy program.js
This presentation:
http://bit.ly/nsclepresentations