Promise and Bluebird

  • View
    1.667

  • Download
    0

Embed Size (px)

Transcript

  • Promise and BluebirdDaniel Ku (http://danielku.com/)

    http://danielku.com/

  • PromiseJavascript

    Promise : https://promisesaplus.com/

    https://promisesaplus.com/

  • Bluebird Promise

    https://github.com/petkaantonov/bluebird

    https://github.com/petkaantonov/bluebird

  • Node.js

    .

    var Promise = require('bluebird');

    .

    var Q = require('bluebird');

    :

  • .

    User.findOne({email: email}, function(err, user) { if (err) { console.error(err); } else { Group.find({owner: user.id}, function(err, groups) { if (err) { console.error(err); } else { try { // do something with groups

    console.log('success'); } catch (err) { console.error(err); } } });});

  • 1. if .

    User.findOne({email: email}, function(err, user) { if (err) { console.error(err); } else { Group.find({owner: user.id}, function(err, groups) { if (err) { console.error(err); } else { try { // do something with groups

    console.log('success'); } catch (err) { console.error(err); } } });});

  • 2. .

    User.findOne({email: email}, function(err, user) { if (err) { console.error(err); } else { Group.find({owner: user.id}, function(err, groups) { if (err) { console.error(err); } else { try { // do something with groups

    console.log('success'); } catch (err) { console.error(err); } } });});

  • Promise . ,

    new Promise(function(resolve, reject) { User.findOne({email: email}, function(err, user) { if (err) reject(err); else resolve(user); });}).then(function(user) { return new Promise(function(resolve, reject) { Group.find({owner: user.id}, function(err, groups) { if (err) reject(err); else resolve(groups); }); });}).then(function(groups) { // do something with groups}).then(function() { console.log('success');}).catch(function(err) { console.error(err);});

  • 1. .

    new Promise(function(resolve, reject) { User.findOne({email: email}, function(err, user) { if (err) reject(err); else resolve(user); });}).then(function(user) { return new Promise(function(resolve, reject) { Group.find({owner: user.id}, function(err, groups) { if (err) reject(err); else resolve(groups); }); });}).then(function(groups) { // do something with groups}).then(function() { console.log('success');}).catch(function(err) { console.error(err);});

  • 2. .

    new Promise(function(resolve, reject) { User.findOne({email: email}, function(err, user) { if (err) reject(err); else resolve(user); });}).then(function(user) { return new Promise(function(resolve, reject) { Group.find({owner: user.id}, function(err, groups) { if (err) reject(err); else resolve(groups); }); });}).then(function(groups) { // do something with groups}).then(function() { console.log('success');}).catch(function(err) { console.error(err);});

  • . ..

    new Promise(function(resolve, reject) { User.findOne({email: email}, function(err, user) { if (err) reject(err); else resolve(user); });});

  • Wrapper .

    var UserWrapper = { findOne: function(query) { return new Promise(function(resolve, reject) { User.findOne(query, function(err, user) { if (err) reject(err); else resolve(user); }); }); }};

  • ..

    var GroupWrapper = { find: function(query) { return new Promise(function(resolve, reject) { Group.find(query, function(err, groups) { if (err) reject(err); else resolve(groups); }); }); }};

  • . .

    UserWrapper.findOne({ email: email }) .then(function(user) { return GroupWrapper.find({ owner: user.id }); }) .then(function(groups) { // do something with groups }) .then(function() { console.log('success'); }) .catch(function(err) { console.error(err); });

  • Promisification , Bluebird Promise.promisify() .

    https://github.com/petkaantonov/bluebird/blob/master/API.md#promisification

  • .

    findUserByEmail(email) .then(findOwningGroups) .then(doSomethingWithGroups) .then(whenSuccess) .catch(whenFail);

    ?

  • ,

  • : Promise Chain

    Promise.defer() deprecated.

    function dont_do_like_this() { var deferred = Promise.defer(); doSomethingAsync(function(err, result) { if (err) return deferred.reject(err); deferred.resolve(result); }); return deferred.promise;}

  • Constructor .

    function ok() { return new Promise(function (resolve, reject) { doSomethingAsync(function(err, result) { if (err) return reject(err); resolve(result); }); });}

  • Callback Promise.try() .

    function checkSomething(something) { return Promise.try(function () { if (is_not_valid(something)) { throw new Error('Something is not valid.'); } return something; });}

  • Promise .

    function startWithValue(value) { return Promise.resolve(value);}

    Promise . ?

    function startWithError(error) { return Promise.reject(error);}

  • : Promise Chain

    Promise .then() .catch() .

    ajaxGetAsync(url) .then(function(result) { return parseValueAsync(result); }) .catch(function(error) { console.error('error:', error); });

  • Promise Promise Chain ..

    somethingAsync() .return(value);

    .

    somethingAsync() .then(function() { return value; });

  • Promise Promise Chain ..

    somethingAsync() .throw(reason);

    .

    somethingAsync() .then(function() { throw reason; });

  • .finally() .

    Promise.resolve('test') .catch(console.error.bind(console)) .finally(function() { return 'finally'; }) .then(function(result) { console.log(result); // result is 'test' });

  • : Promise Chain

    Promise Chain .

  • .done() .

    somethingAsync() .done();

  • Alias

    Promise.try() === Promise.attemp()

    .catch() === .caught()

    .return() === .thenReturn()

    .throw() === .thenThrow()

    .finally() === .lastly()

  • . (X)

    somethingAsync() .then(function(result) { return anotherAsync(result); }, function(err) { handleError(err); });

  • . (O)

    somethingAsync() .then(function(result) { return anotherAsync(result); }) .catch(function(err) { handleError(err); });

  • . (X)

    var promise = somethingAsync();if (is_another_necessary) { promise.then(function(result) { return anotherAsync(result); });}promise.then(function(result) { doSomethingWithResult(result);});

  • . (O)

    var promise = somethingAsync();if (is_another_necessary) { promise = promise.then(function(result) { return anotherAsync(result); });}promise.then(function(result) { doSomethingWithResult(result);});

  • .all()

    .any()

    .some()

    .spread()

  • .

    Promise .all([ pingAsync("ns1.example.com"), pingAsync("ns2.example.com"), pingAsync("ns3.example.com"), pingAsync("ns4.example.com") ]).spread(function(first, second, third, fourth) { console.log(first, second, third, fourth); });

  • .

    Promise .any([ pingAsync("ns1.example.com"), pingAsync("ns2.example.com"), pingAsync("ns3.example.com"), pingAsync("ns4.example.com") ]).spread(function(first) { console.log(first); });

  • .

    Promise .some([ pingAsync("ns1.example.com"), pingAsync("ns2.example.com"), pingAsync("ns3.example.com"), pingAsync("ns4.example.com") ], 2).spread(function(first, second) { console.log(first, second); });

  • .map()

    .reduce()

    .filter()

    .each()

  • .

    Group.listAsync({user: user.id}) .map(function(group) { return group.removeAsync(user.id); }) .then(function() { return user.removeAsync(); });

  • .

    Group.listAsync({user: user.id}) .filter(function(group) { return group.owner_id === user.id; });

  • .map() .filter() concurrency

    Group.listAsync({user: user.id}) .map(function(group) { return group.removeAsync(user.id); }, {concurrency: 1});

  • .

    Group.listAsync({user: user.id}) .reduce(function(promise, group) { return promise.then(function(groups) { return group.removeAsync(user.id) .then(function(group) { groups.push(group); }) .return(groups); }); }, []);

    ( ;;)

  • .delay()

  • .

    Promise .delay(500) .then(function() { return 'Hello world'; }) .delay(500) .then(function(result) { console.log(result); });

  • .tap()

  • Promise Chain ..

    somethingAsync() .tap(function(result_of_somethingAsync) { return anotherAsync(result_of_somethingAsync); }) .then(function(result_of_somethingAsync) { console.log(result_of_somethingAsync); });

  • .bind()

  • ..

    somethingAsync() .spread(function (a, b) { return anotherAsync(a, b) .then(function (c) { return a + b + c; }); });

  • ..

    var scope = {};somethingAsync() .spread(function (a, b) { scope.a = a; scope.b = b; return anotherAsync(a, b); }) .then(function (c) { return scope.a + scope.b + c; });

  • .

    somethingAsync() .bind({}) .spread(function (a, b) { this.a = a;