Promise -fication of JS calls.

Found a new pattern to use recently that is called Promise. I really like the way ES6/7 brings new thinking patterns into life nowdays. Here is Promise used instead of old pattern.

It was to give JS method a callback function. This splitting code and making a lot of possibilities for error to come out in this place.

One would write old times according to MDN:
function greeting(name) {
  alert('Hello ' + name);

function processUserInput(callback) {
  var name = prompt('Please enter your name.');

And now it is made with Promise pattern like so:
let promise = new Promise((resolve, reject) => {
  resolve(prompt('Please enter your name.'));

promise.then((name) => { 
    alert('Hello ' + name); 
In general and briefly this now helps to avoid 'callback hell' with functions passed as arguments and write asynchronous code a sort of in synchronous manner.

This all becomes extra useful upon one having need to load set of data from a different sources. E.g. via several API cals to different url's and react accordingly.

Hail to ES6!


Popular posts from this blog

Python converting PDF to Image

How to disable/enable an element with jQuery or Javascript

Django: Resetting Passwords (with internal tools)