Posts

Showing posts from February, 2018

Promise -fication of JS calls.

Image
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.' ); callback ( name ); } processUserInput ( greeting ); 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 as