Daddy can you keep a Promise()?

Develoger
Develoger
Published in
4 min readMar 17, 2017

--

Would you bring your child to battlefield? Because with a callback you do just that.

This is a story about Promises. The ones you must keep if you care about the people who rely on you.

I knew that this day would eventually come. The warm and sunny weather just made it worse. My grandma’s swinging chair started to creak on the porch.
She was already awake. Her singing voice reached my mind…

Daddy, daddy please don’t go, if (you.leave(me)) { wind will stop to blow; }

My little girl looked up at me with tears in her eyes. She was wearing my mother’s knitted sweater.
I closed my eyes, hugged her tightly and said.

Daddy will come back, like I always do. It’s just a matter of timestamp. Please have trust in me.

It was easier when I was single. Everything I needed to do is to leave without thinking about what will happen. I didn’t plan my next steps, thus the possibility of failure seemed insignificant.
Now I know that it was just mere luck which kept me alive.

Ok, I will trust you if you can keep a Promise() Do that, and even if you fail you will still come back.

The wind was blowing hard. Our old windmill worked like a charm. A silent sound of car in the distance started to become louder.

Baby you know that I can’t. I never gave a Promise() to anyone before. Can we do it with a callback please, I will contact you when I am finished.

The car stopped in front of our house. A cloud of dust made the dog silent for a moment. My daughter looked angry.

I am not little anymore daddy. Callback() is just not enough. It makes you coupled with me, which feels good, but in reality it can hurt us both. Just give me a Promise() and I will know that you will come back once you are done. Then() we will be together again.

Maybe it’s a generation gap, I don’t know. It was always enough to call back. I don’t understand this Promise() thing. Still, my baby girl is asking for it. If it’s important for her, it must be good.
A car siren started to honk.

Ok my lady, I will do it for you. I will Promise() no matter how strange or undefined it is to me. Let me just change what I already packed in the suitcase…

Removing old luggage

var daddySafeCallback = function(comeback) {
console.log(comeback);
};
var daddyUnsafeCallback = function(message) {
console.log(message);
};
function Daddy(safeCallback, unsafeCallback) {
if (safe) {
safeCallback('I am back');
} else {
unsafeCallback('I will always love you');
}
};
Daddy(daddySafeCallback, daddyUnsafeCallback);

Putting new things in

let Daddy = new Promise((resolve, reject) => {
if (safe) {
resolve('I am back');
} else {
reject('I will always love you');
}
});

Daddy.then((comeback) => {
console.log(comeback);
}).catch((message) => {
console.log(message);
});

It felt great once I packed the last item into the suitcase. I thought it would be tougher than it actually was.
My girl started to smile.

Now you promised daddy. I know that it alone is not making things more secure. Still, it’s better than a mere callback.
When you promise, I know that the outcome is not coupled with how your mind works. You just promised that you’ll get back to me when you are done. I will always be able to handle what happens next, without changing your promise. Because I know that I can trust you.

Promises are made to be kept.

Epilogue

Story aside, callback is not a feature of ES5. It is important to know that callbacks are in fact an implementation of first class functions and that’s it.
The bad side of callbacks is that their execution is coupled with the function to which they are passed.

A Promise on the other hand is a feature implemented in ES2015. The idea behind it is based on callback logic, but the final result is quite different.
A Promise will be resolved or rejected, it doesn’t actually know what will be implemented in a then() or catch(). That scenario makes it more decoupled, when compared to a callback implementation.

So… listen to your child, give and keep Promise(). Since sometimes calling back is just not enough.

At the end there is something even better and it’s called async / await functions… But let’s leave that for another place and time.

If you are reading this it means that you are curious and passionate about JavaScript and Front-End in general. That makes two of us!
Consider joining me on this adventure of discovery by following me here, clicking on that heart, or checking out my twitter account https://twitter.com/develoger

Once I hit 1000 followers on the blog I will share the prizes (public draw) -- 2 yearly subscriptions to https://egghead.io

--

--