Callbacks to async
Launch week, Tuesday: the payments module goes async, on a branch. The dot formula for the same small edit at every function (n.), :%s with a group where the change has a pattern, and a list of what's left before it merges.
Tuesday of launch week, 9:00 a.m. The payments module still passes callbacks around, and Burrito 2.0 wants async and await. The payments client got its v3 last week: leave the callback out and a call returns a promise instead. So today the module goes all the way, on a branch: every function async, no done parameter, return await on each call, and the callback gone.
Three functions today, thirty in the real module. Whatever you do once, you'll want to repeat.
Warm-up from earlier chapters
Two from earlier chapters, from memory, before the new idea. Two wrong tries show the answer.
Repeat the small change, send the patterned one
Two kinds of change here. The same small edit at many places (async in front of each function) is the dot formula from week one: search once, make the change, then n . n .. A change with a pattern in it (every call losing its callback, whatever its arguments) is a slip: :%s with a circled group and \1.
export function voidOrder(order, done) { payments.void(order.id, (err, voided) => { if (err) return done(err); done(null, voided); });}export async function voidOrder(order, done) { payments.void(order.id, (err, voided) => { if (err) return done(err); done(null, voided); });}export async function voidOrder(order) { payments.void(order.id, (err, voided) => { if (err) return done(err); done(null, voided); });}export async function voidOrder(order) { return await payments.void(order.id); if (err) return done(err); done(null, voided); });}export async function voidOrder(order) { return await payments.void(order.id);}async, no done, return await on the call, and the callback's leftovers gone. A rejected promise throws on its own, so the if (err) line has nothing left to do.Stage 1 · async
9:10. async in front of every function. Search for function, change it with cw to async function, then n . for the other two. (i would leave the cursor before function, and n would find the same one again.)
This exercise needs a keyboard. Read on, and play it at your computer.
Stage 2 · No done
9:30. The , done parameter leaves every signature: chargeOrder(order, done) becomes chargeOrder(order), and the same for the other two. One change, then the same formula as stage 1.
Stuck? Show a hint
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis exercise needs a keyboard. Read on, and play it at your computer.
Stage 3 · return await
10:00. Each call loses its callback and gets return await in front: payments.void(order.id, (err, voided) => { becomes return await payments.void(order.id);. The three calls have different arguments, so it's a slip: circle everything before , (err, and put it back.
Stuck? Show a hint
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis exercise needs a keyboard. Read on, and play it at your computer.
Stage 4 · The leftovers
10:20. Under each return await line sit three leftovers of the callback: the if (err) line, the done(null, …) line and the });. Clear all nine lines; every function ends in its own }.
Stuck? Show a hint
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis exercise needs a keyboard. Read on, and play it at your computer.
Mechanical tools, from memory
Press the keys.