SlideShare a Scribd company logo
Async/Await functions in Ruby
Sat, Dec. 14th, 2019 @平成Ruby会議01
@sat0yu
- Software engineer at
- Building microservices with
gRPC and GraphQL
- Three years of
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
GraphQL as an integrator
*Note: just a part of the whole Quipper services
N+1 problems with GraphQL
Async await functions in ruby
ref. https://github.com/rmosolgo/graphql-ruby/blob/master/guides/schema/lazy_execution.md
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
Nested loaders 🤔
ref. https://github.com/Shopify/graphql-batch
“Promise” in Graphql-batch💡
ref. https://github.com/Shopify/graphql-batch
- 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
rejectpending
fulfilled
rejected
※ .load(...) returns a promise
Async/Await syntax with Promise and Genarator
JavaScript
Async/Await in JavaScript
- Declared in ES2017(ES8); Almost all browsers have compatibility
- Convenient to write async processing with a synchronized-ish syntax
JavaScript
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
Promise + Generator ≈ Async/Await
≈
JavaScript
Generator pretends Pause and Resume
JavaScript
Generator pretends Pause and Resume
JavaScript
How can we use Pause & Resume in Ruby?
Generator pretends Pause and Resume
JavaScript
Fiber (1.9~)
What is “Fiber”?
⏸
⏩
What is “Fiber”?
⏩
What is “Fiber”?
⏩
What is “Fiber”?
⏸
What is “Fiber”?
⏩
What is “Fiber”?
⏩
What is “Fiber”?
⏩
What is “Fiber”?
⏸
What is “Fiber”?
Async/Await syntax with Promise and Genarator
Fiber
2. Rewrite nested loaders with Fiber
1. Nested loaders implementation 3. Rewrite nested “resume”s
1. Nested loaders implementation
2. Rewrite nested loaders with Fiber
3. Rewrite nested “resume”s
1. Nested loaders implementation
1. Nested loaders implementation
1. Nested loaders implementation
1. Nested loaders implementation
1. Nested loaders implementation
2. Rewrite nested loaders with Fiber
3. Rewrite nested “resume”s
2. Rewrite nested loaders with Fiber
⏸
2. Rewrite nested loaders with Fiber
⏩
2. Rewrite nested loaders with Fiber
⏩
p1
2. Rewrite nested loaders with Fiber
⏸
p1
next
p1
2. Rewrite nested loaders with Fiber
⏸
p1
next
p1
2. Rewrite nested loaders with Fiber
⏩
p1
next
p1
2. Rewrite nested loaders with Fiber
⏩
p1
next
p1
p2
2. Rewrite nested loaders with Fiber
⏸
p1
next
p1
p2
next
p2
2. Rewrite nested loaders with Fiber
⏸
p1
next
p1
p2
next
p2
2. Rewrite nested loaders with Fiber
⏩
p1
next
p1
p2
next
p2
2. Rewrite nested loaders with Fiber
p1
next
p1
p2
next
p2
X
2. Rewrite nested loaders with Fiber
p1
next
p1
p2
next
p2
X
2. Rewrite nested loaders with Fiber
Looks
working
🎉👏
2. Rewrite nested loaders with Fiber
1. Nested loaders implementation
2. Rewrite nested loaders with Fiber
3. Rewrite nested “resume”s
3. Rewrite nested “resume”s
3. Rewrite nested “resume”s
3. Rewrite nested “resume”s
3. Rewrite nested “resume”s
3. Rewrite nested “resume”s
3. Rewrite nested “resume”s
3. Rewrite nested “resume”s
Looks
working
🎉👏
3. Rewrite nested “resume”s
Let’s apply the naive Async/Await syntax to xx
Async/Await in
When fetching A and B fields
When fetching C and D fields
N+1…🤔
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
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
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 🙇

More Related Content

PPTX
Kubernetes London - Musings on Kubernetes Operators
PDF
"fireap" - fast task runner on consul
PDF
AnyEvent and Plack
ODP
Extreme JavaScript Minification and Obfuscation
PDF
Kubernetes at Datadog Scale - Ara Pulido
PDF
Python performance engineering in 2017
PDF
Traceur - Javascript.next - Now! RheinmainJS April 14th
PDF
How to deploy your Rails application on Windows
Kubernetes London - Musings on Kubernetes Operators
"fireap" - fast task runner on consul
AnyEvent and Plack
Extreme JavaScript Minification and Obfuscation
Kubernetes at Datadog Scale - Ara Pulido
Python performance engineering in 2017
Traceur - Javascript.next - Now! RheinmainJS April 14th
How to deploy your Rails application on Windows

What's hot (20)

PDF
Celebrating Guile 2020. Lessons Learned in the Last Lap to Guile 3 (FOSDEM 2020)
PDF
JavaScriptCore's DFG JIT (JSConf EU 2012)
PDF
PyParis2017 / Tutorial transcript - Function-as-a-service : a pythonic perspe...
PDF
ES6 ECMA2015
PPTX
News from EGit - Talk EclipseCon Europe 2014 - Ludwigsburg
PPTX
Boosting your kubectl productivity @ KubeCon 19 NA
PDF
Types - slice, map, new, make, struct - Gopherlabs
KEY
Concurrency in ruby
ODP
(entregando djangoapps)@tangerinalab - pugpe xv
PDF
Openbravo live development builds
PDF
Simple Tips and Tricks with Ansible
PPT
Quartz.NET - Enterprise Job Scheduler for .NET Platform
PDF
Event Machine
PDF
Devoxx UK 2014 High Performance In-Memory Java with Open Source
PDF
Ansible Israel Kickoff Meetup
PPTX
Cool new stuff in JGit and EGit - Eclipse Democamp Munich 2013-06-12
KEY
W3C HTML5 KIG-How to write low garbage real-time javascript
PPTX
Quartz in mule
PDF
Python meetup 2
Celebrating Guile 2020. Lessons Learned in the Last Lap to Guile 3 (FOSDEM 2020)
JavaScriptCore's DFG JIT (JSConf EU 2012)
PyParis2017 / Tutorial transcript - Function-as-a-service : a pythonic perspe...
ES6 ECMA2015
News from EGit - Talk EclipseCon Europe 2014 - Ludwigsburg
Boosting your kubectl productivity @ KubeCon 19 NA
Types - slice, map, new, make, struct - Gopherlabs
Concurrency in ruby
(entregando djangoapps)@tangerinalab - pugpe xv
Openbravo live development builds
Simple Tips and Tricks with Ansible
Quartz.NET - Enterprise Job Scheduler for .NET Platform
Event Machine
Devoxx UK 2014 High Performance In-Memory Java with Open Source
Ansible Israel Kickoff Meetup
Cool new stuff in JGit and EGit - Eclipse Democamp Munich 2013-06-12
W3C HTML5 KIG-How to write low garbage real-time javascript
Quartz in mule
Python meetup 2
Ad

Similar to Async await functions in ruby (20)

PDF
[k8s] Kubernetes terminology (1).pdf
PDF
Boxen: How to Manage an Army of Laptops
PDF
Java and Container - Make it Awesome !
PPTX
Laravel, docker, kubernetes
PDF
Polyglot, fault-tolerant event-driven programming with kafka, kubernetes and ...
PDF
K8s is not for App Developers
PDF
InfluxCloudi craft container orchestrator
PDF
Kernel load-balancing for Docker containers using IPVS
PDF
Networking in Kubernetes
PDF
How to build 1000 microservices with Kafka and thrive
PDF
Kubernetes Basis: Pods, Deployments, and Services
PDF
Cloud read java with kubernetes
PDF
You got database in my cloud!
PDF
Katello on TorqueBox
PDF
9 steps to install and configure postgre sql from source on linux
PDF
[KubeCon EU 2020] containerd Deep Dive
PPT
Triple Blitz Strike
PPTX
Building Docker Containers @ Scale
PDF
Polyglot, Fault Tolerant Event-Driven Programming with Kafka, Kubernetes and ...
PDF
Making kubernetes simple for developers
[k8s] Kubernetes terminology (1).pdf
Boxen: How to Manage an Army of Laptops
Java and Container - Make it Awesome !
Laravel, docker, kubernetes
Polyglot, fault-tolerant event-driven programming with kafka, kubernetes and ...
K8s is not for App Developers
InfluxCloudi craft container orchestrator
Kernel load-balancing for Docker containers using IPVS
Networking in Kubernetes
How to build 1000 microservices with Kafka and thrive
Kubernetes Basis: Pods, Deployments, and Services
Cloud read java with kubernetes
You got database in my cloud!
Katello on TorqueBox
9 steps to install and configure postgre sql from source on linux
[KubeCon EU 2020] containerd Deep Dive
Triple Blitz Strike
Building Docker Containers @ Scale
Polyglot, Fault Tolerant Event-Driven Programming with Kafka, Kubernetes and ...
Making kubernetes simple for developers
Ad

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Spectroscopy.pptx food analysis technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPT
Teaching material agriculture food technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Chapter 3 Spatial Domain Image Processing.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
KodekX | Application Modernization Development
PDF
Review of recent advances in non-invasive hemoglobin estimation
cuic standard and advanced reporting.pdf
NewMind AI Weekly Chronicles - August'25 Week I
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
sap open course for s4hana steps from ECC to s4
Spectroscopy.pptx food analysis technology
20250228 LYD VKU AI Blended-Learning.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Teaching material agriculture food technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Machine learning based COVID-19 study performance prediction
MIND Revenue Release Quarter 2 2025 Press Release
Chapter 3 Spatial Domain Image Processing.pdf
The AUB Centre for AI in Media Proposal.docx
Dropbox Q2 2025 Financial Results & Investor Presentation
Spectral efficient network and resource selection model in 5G networks
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KodekX | Application Modernization Development
Review of recent advances in non-invasive hemoglobin estimation

Async await functions in ruby