Upgrade to Pro — share decks privately, control downloads, hide ads and more …

promise

 promise

3925ee6eaa41031bac799de0f4f528ec?s=128

to4iki

May 20, 2015
Tweet

Transcript

  1. Promiseͱ͔ 2015—05-18 toshiki takezawa @to4iki

  2. ୭ʁ • ஛ᖒढ़ق • 2012/4 • Ruby, Scala, Swift, JavaScript

    • ·ͨ൅Λ৳͹ͦ͏ͱࢥͬͯͨΓ
  3. Կ΍ͬͯΜͷɺ΍ͬͯͨʁ • WebαΠτͷJava -> RubyͷϦϓϨΠ • ιγϟήͷAPI࡞੒ • ిࢠϚϯΨͷϓϥοτϑΥʔϜӡӦ •

    ઌ݄iOSΞϓϦΛϦϦʔε͠·ͨ͠
  4. What is Promise?

  5. Promise ͱݺ͹ΕΔ΋ͷ͕͋ΔΒ͍͠ݴޠ • ECMAScript 6 (Promises A/+) • jQuery.Deferred •

    Java (java.util.concurrent.Future) • Scala (scala.concurrent.Future)
  6. Promise<T> • ඇಉظॲཧΛந৅Խͨ͠ΦϒδΣΫτͱͦΕ Λૢ࡞͢Δ࢓૊Έ • কདྷతʹ "ਖ਼ৗͳ஋" or "ҟৗͳ஋" ͕ೖΔࣄΛ༧໿ͨ͠ΦϒδΣΫτ

    • ex. APIͷϦΫΤετ͕ฦ͖ͬͯͨΒ… • ͍ΘΏΔσβΠϯύλʔϯͷͻͱͭ
  7. ඇಉظ?

  8. ྫ. Α͋͘Δඇಉظॲཧ (JavaScriptͰ)

  9. callback pattern getA(function(error, a) { // Aऔಘࣦഊ࣌ͷॲཧ if (error) {

    throw error; } getB(function(error, b) { // Bऔಘࣦഊ࣌ͷॲཧ if (error) { throw error; } getC(function(error, c) { // Cऔಘࣦഊ࣌ͷॲཧ if (error) { throw error; } // a, b, cऔಘ੒ޭ࣌ͷॲཧ done(); }); }); });
  10. None
  11. promise pattern getA() .then(getB()) .then(getC()) .then(done()) .catch(function(e) { throw e;

    })
  12. Example.

  13. ex. getURL function getURL(URL) { return new Promise(function (resolve, reject)

    { var req = new XMLHttpRequest(); req.open('GET', URL, true); req.onload = function () { if (req.status === 200) { resolve(req.responseText); // ੒ޭ࣌ } else { reject(new Error(req.statusText)); // ࣦഊ࣌ } }; req.onerror = function () { reject(new Error(req.statusText));ɹ// ࣦഊ࣌ }; req.send(); }); } • PromiseίϯετϥΫλ͔ΒPromiseΦϒδΣΫτ(Πϯελϯε)Λ࡞Γฦ͢ • ίϯετϥΫλͷதͰ(ඇಉظ)ॲཧΛߦ͏ • ॲཧͷ੒൱ʹΑͬͯɺresolve<݁Ռͷ஋> ·ͨ͸ reject<ErrorΦϒδΣΫτ> ΛݺͿ
  14. PromiseΦϒδΣΫτ var URL = "http://httpbin.org/get"; getURL(URL).then(function onFulfilled(value){ console.log(value); }).catch(function onRejected(error){

    console.error(error); }); • promiseΦϒδΣΫτʹͦΕͧΕcallbackΛొ࿥ • then Ͱ੒ޭ࣌ʹݺͼग़͢callback(fulfill͞Εͨ) • catchͰࣦഊ࣌ʹݺͼग़͢callback(reject͞Εͨ) • PromiseίϯετϥΫλͷॲཧ݁ՌʹΑͬͯͲͪΒ͔͕ݺ͹ΕΔ
  15. Promise͸஋ͷശ • ࠓ͸·ͩͳ͍͚Ͳকདྷతʹத਎ʢ஋ʣ͕ಘΒΕΔശ • resolve<T> or reject<Error> • ஋͕ೖͬͨ࣌ʹ then

    or catchΛݺͿͱ͍͏ػೳΛ΋ͬͨശ • Optionalͱಉ͡(த਎ʢ஋ʣ͕ۭ͔΋͠Εͳ͍ശ) • Some<T> or None • Eitherͱಉ͡(த਎ʢ஋ʣ͕Τϥʔ͔΋͠Εͳ͍ശ) • Left<Error> or Right<T>
  16. Promiseͷঢ়ଶ͸3͚ͭͩ • Fulfilled • resolve(੒ޭ)ͨ࣌͠ɺ͜ͷ࣌thenͰొ࿥ͨ͠callback͕ݺ͹ΕΔ • Rejected • reject(ࣦഊ)ͨ࣌͠ɻ͜ͷ࣌ catchͰొ࿥ͨ͠ίʔϧόοΫ

    ͕ݺ͹ΕΔ • Pending • resolve·ͨ͸rejectͰ͸ͳ͍࣌ɻpromiseΦϒδΣΫτ͕࡞੒͞Εͨॳظঢ়ଶ౳͕֘౰͢Δ
  17. Promise͸࢖͍ࣺͯΦϒδΣΫτ • promiseΦϒδΣΫτ͸Ұ౓ঢ়ଶ͕มߋͨ͠ΒͦΕҎ ߱͸ෆม • ͭ·Γɺಉ͡PromiseΦϒδΣΫτΛ࢖͍·Θ͞ͳ͍ (immutable) • then΋ৗʹ৽͍͠ΦϒδΣΫτΛ࡞੒ͯ͠ฦ͢

  18. ࿈࠯తʹॻ͚Δ(promise chain) Promise.resolve(2) .then(function(v) { return v * 2; })

    .then(function(v) { return v * v; }) .then(function(v) { console.log(v); }) .catch(function(e) { // Ͳ͔͜Ͱerrror͕ى͖ͨΒ͜͜Ͱcatch͞ΕΔ console.error(e); }) • then΍catch͸ϝιουνΣʔϯͳͷͰ࿈ଓͰॻ͚Δ • then΍catch͸ຖճ৽͍͠promiseΦϒδΣΫτΛฦͯ͠࿈࠯͢Δ
  19. Promiseͱerror handling • ίϯετϥΫλͰrejectΛฦ͢ͱcatch͞ΕΔ • Promise಺Ͱى͖ͨΤϥʔ͸ࣗಈతʹtry-catch͞ΕɺPromiseͰcatchͰ͖ Δ • ͦΕͧΕݸผͷॲཧ͔Β͸ΤϥʔϋϯυϦϯά͕ͳ͘ͳΔ(reject͢Δ͚ͩ) •

    ٯʹ.catchΛॻ͖๨ΕΔͱԿ΋ݴΘͳ͘ͳͬͯ͠·͏ • PromiseͰ͸ඇಉظॲཧʹ໨͕ߦ͖͕͕ͪͩΤϥʔϋϯυϦϯάͷํΛ༏ ઌͨ͠
  20. ࢖͍ॲ • PromiseΛ࢖͏͜ͱͰɺॲཧΛந৅తͳ஋ͱͯ࣋ͭ͜͠ͱ͕ՄೳʹͳΔ • ͦΕͧΕͷॲཧΛPromiseͰϥοϓͯ͠ɺ૊Έ߹ΘͤΔ͚ͩͰྑ͘ͳ Δ • ඇಉظAPIͰೝূ͔ͯ͠ΒͰͳ͍ͱୟ͚ͳ͍APIΛ࢖͏࣌ • ϩάΠϯAPI

    + ඇಉظ௨৴API • ඇಉظॲཧͷΤϥʔϋϯυϦϯάͷ؆ܿԽ • callbackͩͱશͯࣗ෼Ͱ΍Δඞཁ͕͋Δ
  21. ·ͱΊ • Promise͸ඇಉظॲཧΛPromiseΦϒδΣΫτ (ະདྷͷ஋Λ֨ೲͨ͠ശ)ͱͯ͠ѻ͑Δ • ෳ਺ͷඇಉظॲཧΛ·ͱΊΔ࣌౳ʹ༗ޮ • Promise͸ڧ͍ΤϥʔϋϯυϦϯάΛ࣋ͬͯΔ • Promise͸ສೳͰ͸ͳ͍ͷͰదࡐదॴ

  22. ͋Γ͕ͱ͏͍͟͝·ͨ͠

  23. see also • Promise - JavaScript | MDN • https://developer.mozilla.org/ja/docs/Web/JavaScript/

    Reference/Global_Objects/Promise • JavaScript Promiseͷຊ • http://azu.github.io/promises-book • ࠓߋ͚ͩͲPromiseೖ໳ • http://qiita.com/koki_cheese/items/c559da338a3d307c9d88