Contenu connexe Similaire à Miroslav Bajtoš - Nativní async/await v Node.js - už tam jsme? (20) Miroslav Bajtoš - Nativní async/await v Node.js - už tam jsme?3. it('creates a product in a category', function(done) {
Category.create({name: 'Food'}, function(err, cat) {
if (err) return done(err);
cat.products.create({name: 'Apple'}, function(err, prod) {
if (err) return done(err);
expect(prod).to.have.property('categoryId', cat.id);
done();
});
});
});
ES5 - důvěrné známe jistoty
4. it('creates a product in a category', function(done) {
Category.create({name: 'Food'}, function(err, cat) {
if (err) return done(err);
cat.products.create({name: 'Apple'}, function(err, prod) {
if (err) return done(err);
expect(prod).to.have.property('categoryId', cat.id);
done();
});
});
});
S červíkem pochybností
6. it('creates a product in a category', function(done) {
var cat;
async.waterfall([
function(next) {
Category.create({name: 'Food'}, next);
},
function(c, next) {
cat = c;
cat.products.create({name: 'Apple'}, next);
},
function(prod, next) {
expect(prod).to.have.property('categoryId', cat.id);
next();
}
], done);
});
ES5 - caolan/async
7. it('creates a product in a category', function() {
return Category.create({name: 'Food'}).then(cat => {
return cat.products.create({name: 'Apple'}).then(prod => {
expect(prod).to.have.property('categoryId', cat.id);
});
});
});
ES6/ES2015 - Promises
8. it('creates a product in a category', async function() {
const cat = await Category.create({name: 'Food'});
const prod = await cat.products.create({name: 'Apple'});
expect(prod).to.have.property('categoryId', cat.id);
});
ES2017 - async/await
9. function one() {
return Promise.resolve(1);
}
async function two() {
const i = await one();
return i+1;
}
two().then(n => console.log(n));
Snadné kombinování
11. Standardizace v ECMAScript
Leden 2014: začíná diskuse v TC39
Srpen 2015: stage 2 (“draft”)
Říjen 2015: stage 3 (“candidate”)
Říjen 2016: stage 4 (“finished”)
12. Node.js
Říjen 2016: Node.js 7.0.0 (V8 5.4)
$ node --harmony async-await.js
Únor 2017: Node.js 7.6.0 (V8 5.5)
$ node async-await.js
Duben 2017: Node.js 8.0.0-RC (V8 5.7)
výrazné zvýšení výkonu
13. async function two() {
const i = await one();
throw new Error('test');
}
Error: test
at two (err-stack.js:3:9)
at process._tickCallback (next_tick.js:109:7)
...
Stack trace - await
14. function two() {
return one().then(() => {
throw new Error('test');
});
}
Error: test
at one.then (err-stack.js:3:9)
at process._tickCallback (next_tick.js:109:7)
...
Stack trace - Promises
18. Interoperabilita s Bluebird
const Promise = require('bluebird');
const one = () => Promise.resolve(1);
one() instanceof Promise // true
const two = async () => await one() + 1;
two() instanceof Promise // false
19. Mé výhrady k transpilaci
- Složitější nastavení projektu
- Čekání na transpilátor - pomalejší TDD
- Stack traces z transpilovaného kódu
- Ztráta vychytávek nativní verze await
20. Kdy přejít na async/await (nativně)
- Můžete vyžadovat Node.js 8+
- Výkon kódu není kritický
- Stačí vám ES6 Promise API
(bez rozšíření Bluebird)
24. Co na to Java?
Icon made by Freepik from www.flaticon.com