To have a better understanding of the differences between Promises and traditional callbacks, consider an asynchronous task like this:
function process(callback) { stepOne((error, resultOne) => { if (error) { callback(error); return; } stepTwo(resultOne, (error, resultTwo) => { if (error) { callback(error); return; } callback(undefined, resultTwo + 1); }); }); }
If we write preceding above in Promise style, it would be as follows:
function process() { return stepOne() .then(result => stepTwo(result)) .then(result => result + 1); }
As in the preceding example, Promise makes it easy and natural to write asynchronous operations with a flat chain instead of nested callbacks. But the most exciting thing about Promise might be the benefits it...