const retry = require(‘p-retry')
// retry 2 times before giving up
retry(loadFeed, {
retries: 2
})
// everything is still a promise
function loadFeed() {
return get(feedURL).then(feedRes => {
return feedRes.json()
})
}
Slide 50
Slide 50 text
failures + latency
Service
Slide 51
Slide 51 text
timeouts + retries?
Slide 52
Slide 52 text
99pct ~ 1500ms
avg ~ 200ms
median ~ 150ms
Slide 53
Slide 53 text
most responses are fast!
~ 200ms
slow responses are actually
pretty rare
Slide 54
Slide 54 text
~3 x 250ms ~= 750ms
1 x 1500ms ~= 1500ms
Slide 55
Slide 55 text
No content
Slide 56
Slide 56 text
// avg response time is ~200ms
const feedTimeout = 250
pTimeout(
loadFeed,
feedTimeout
)
.then(onSuccess)
.catch(onError)
// I have 100 queries to run
const queries = […]
const pLimit = require(‘p-limit’)
// Only 10 promises at a time
const only10 = pLimit(10)
const promises = queries.map(q => {
return only10(
() => fetchAnalyticsQuery(q)
)
})
Promise.all(promises).then(onSuccess)
Slide 64
Slide 64 text
const sleep = require('then-sleep')
// Or something a bit simpler
while (condition) {
await runAnalyticsQuery(...)
await sleep(250)
}
Slide 65
Slide 65 text
other cool ideas
• reducing bursts with jittering: then-sleep, p-defer
• priority queues: p-queue
• circuit breaking: opossum
Slide 66
Slide 66 text
Promises (and async/await)
are simple concurrency
primitives
with a relatively low barrier of entry
Slide 67
Slide 67 text
but they also
have limitations
• cancellations
• error handling can be tricky
• stack traces aren’t perfect
Slide 68
Slide 68 text
Even more stuff!
• param validation: https://github.com/nettofarah/
property-validator
• testing: https://github.com/nettofarah/axios-vcr
Slide 69
Slide 69 text
for a longer list of promise
awesomeness
https://github.com/sindresorhus/promise-fun
Slide 70
Slide 70 text
for more sophisticated
async programming
primitives
rx.js
check out
Slide 71
Slide 71 text
start with
the simplest solution
you can think of
and then grow it from there