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

Async/Await functions in Ruby

Avatar for Yusuke Sato Yusuke Sato
December 14, 2019

Async/Await functions in Ruby

## What’s takeaway of this session

- The basic use of Promise and Fiber
- The combination of Promise and Fiber performs like Async/Await syntax
- The naive Async/Await syntax sadly does NOT work efficiently in Graphql-ruby

Avatar for Yusuke Sato

Yusuke Sato

December 14, 2019

Other Decks in Programming

Transcript

  1. What’s takeaway of this session 1. The basic use of

    Promise and Fiber 2. ★ The combination of Promise and Fiber performs like Async/Await syntax 3. ★★ The naive Async/Await syntax sadly does NOT work efficiently in ★: hard for @sat0yu to explain
  2. How to use Graphql-batch - Build a loader so as

    inherit GraphQL::Batch::Loader - which need implementing perform(...)fetching data in bulk - Generate field values through the loader X.rb Y.rb ref. https://github.com/Shopify/graphql-batch
  3. - A representation of an eventual value - The value

    won’t be calculated until evaluating it (lazy-evaluation) - Chain-able with other promises through .then(...) What is “Promise”? fulfill reject pending fulfilled rejected ※ .load(...) returns a promise
  4. Async/Await in JavaScript - Declared in ES2017(ES8); Almost all browsers

    have compatibility - Convenient to write async processing with a synchronized-ish syntax JavaScript
  5. Pause and Resume for promises - Declared in ES2017(ES8); Almost

    all browsers have compatibility - Convenient to write async processing with a synchronized-ish syntax sleeping... Pause Resume JavaScript
  6. What’s wrong with the naive Async/Await? The naive Async/Await impl.

    returns a value, not a promise as a result Graphql-batch allows fields to return a promise in place of a value
  7. Is it possible to fix it? - It makes me

    feel it’s do-able… but it does not work even if modifying it as returning a promise - The main difficulty comes from the difference in the promise life-cycle between Graphql-batch and the handcrafted Async/Await implementation
  8. Summary - Explained the basic usage of Promise and Fiber

    - Confirmed the combination of Promise and Fiber works as Async/Await syntax - Observed that the naive impl. is not enough to work along with Graphql-batch - Code available: https://github.com/sat0yu/heisei-ruby-kaigi01 Thank you for your time and attention