Learn Practice Daily golf Reference
Real-world refactors·Lesson 2 of 6·12 min·0/6 exercisesPro

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.

Scene

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.

From memory

Warm-up from earlier chapters

Two from earlier chapters, from memory, before the new idea. Two wrong tries show the answer.

Search for the word under the cursor.
press the keys…
1 of 2
Search for the word under the cursor.

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.

Before
export function voidOrder(order, done) {
payments.void(order.id, (err, voided) => {
if (err) return done(err);
done(null, voided);
});
}
/function↵cwasync functionEsc
export async function voidOrder(order, done) {
payments.void(order.id, (err, voided) => {
if (err) return done(err);
done(null, voided);
});
}
/, done↵dt)
export async function voidOrder(order) {
payments.void(order.id, (err, voided) => {
if (err) return done(err);
done(null, voided);
});
}
:%s/\(payments.*\), (err.*$/return await \1);/↵
export async function voidOrder(order) {
return await payments.void(order.id);
if (err) return done(err);
done(null, voided);
});
}
/if (err↵3dd
export async function voidOrder(order) {
return await payments.void(order.id);
}
One function, four steps: 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.
Boss round

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.)

Fix the marked spots · 3 left
1export function chargeOrder(order, done) {
1 payments.charge(order.id, order.total, (err, receipt) => {
2 if (err) return done(err);
3 done(null, receipt);
4 });
5}
6
7export function refundOrder(order, done) {
8 payments.refund(order.id, (err, refund) => {
9 if (err) return done(err);
10 done(null, refund);
11 });
12}
13
14export function voidOrder(order, done) {
15 payments.void(order.id, (err, voided) => {
16 if (err) return done(err);
17 done(null, voided);
18 });
19}
NORMALLoading the editor…0.0s1:1

This exercise needs a keyboard. Read on, and play it at your computer.

Boss round

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
Pro
The rest of this lesson is in Pro

The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.

See Pro

This exercise needs a keyboard. Read on, and play it at your computer.

Boss round

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
Pro
The rest of this lesson is in Pro

The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.

See Pro

This exercise needs a keyboard. Read on, and play it at your computer.

Boss round

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
Pro
The rest of this lesson is in Pro

The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.

See Pro

This exercise needs a keyboard. Read on, and play it at your computer.

Async, on its branchFinish the exercise above to see it.
From memory

Mechanical tools, from memory

Press the keys.

To the next match of your search, and repeat the last change there.
scroll here, then press the keys2 keys
1 of 3
To the next match of your search, and repeat the last change there.