šÆ JavaScript Promisifying: Ordering Akara with a Callback vs a Promise
š½ļø The Callback Way: Mama Akaraās Corner
Imagine youāre super hungry on a Saturday morning, and you decide to go get Akaraāthose delicious fried bean cakesāfrom Mama Akaraās roadside spot. You walk up to her and say:
āMama, I want ā¦200 Akara!ā
Mama Akara smiles and replies:
āNo wahala my pikin! Come back in 10 minutesāI go call you when e ready.ā
So, you go sit down nearby, maybe watching the okada riders zoom by. But your ears are always alertāwaiting for Mama Akara to shout:
āAkara don ready o! Come collect am!ā
That shout is like a callback. You left your name with Mama Akara (a function), and when the Akara was ready, she called you back with the result.
š§ The Promise Way: The Fancy Akara App
Now imagine thereās a new joint in townāAkaraXpressāand they do things with technology.
You place your order online using their app. Right away, you get a tracking link that says:
āTrack your Akara here. Youāll be notified when itās ready.ā
That tracking link is a Promise!
- While you wait, you can relax, read comics, or play video games.
- When the Akara is ready, the app pings youā
.then()
- If something goes wrong (they run out of beans or kerosene š¬), you get an error messageā
.catch()
You can even await the Akara like this:
const akara = await orderAkara();
console.log("Yay! My Akara is here:", akara);
Thatās comfort and code all in one!
š ļø So⦠What Is Promisifying?
Letās say Mama Akara (the callback shop) wants to upgrade and reach more customers.
She doesnāt stop frying Akara her usual way, but you build her a simple app that wraps her old process with a modern Promise-based tracking feature.
Thatās promisifying.
You didnāt change how she fries the Akaraāyou just made it easier for people to know when itās ready without hanging around all day.
š» In Code: Akara Callback vs Promisified Akara
// Callback-style function
function orderAkara(amount, callback) {
setTimeout(() => {
if (amount)
callback(null, `Here is your ā¦${amount} Akara š«`);
else
callback("No amount specified!");
}, 1000);
}
// Promisified version
function orderAkaraPromise(amount) {
return new Promise((resolve, reject) => {
orderAkara(amount, (err, result) => {
if (err) reject(err);
else resolve(result);
});
});
}
// Using the Promise
orderAkaraPromise(200)
.then(akara => console.log("š", akara))
.catch(error => console.error("ā", error));
š§ In a Nutshell
- Callbacks = Waiting for Mama Akara to call you back
- Promises = Getting a link from AkaraXpress to track your order
- Promisifying = Helping Mama Akara use a tracking app without changing how she fries Akara
Itās all about making old code work with new systems, just like helping small businesses go digital!
š® Practice
Letās test your knowledge with these tasty questions:
- Whatās the difference between Mama Akaraās callback and AkaraXpressās promise?
- Can you think of something else in real life that works like a callback?
- What does it mean to āpromisifyā Mama Akaraās business?
- If you were designing an Akara delivery app, would you use callbacks or promises? Why?
-
Try converting this sandwich function into a Promise:
function makeSandwich(ingredients, callback) { if (!ingredients.length) callback("No ingredients!"); else callback(null, "Sandwich ready!"); }