3 ms·
I recently started programming more using promises and I can say that I am very satisfied with the way it does away with the callback hell problem. Instead of
by pmalynin 11y ago
I recently started programming more using promises and I can say that I am very satisfied with the way it does away with the callback hell problem.
Instead of taking a callback, a function returns a promise, which can be daisy-chained to do work.
Ex:
file.read().then(console.log);
Or using the example in the article:
var a = fileA.read();
var b = fileB.read();
promise.all([a,b]).then((data) => console.log(data[0], data[1]));
- iamstef 11y agojust wait till you try generators + async/await, it can be a lovely experience. No callbacks, and working try/catch.
- LePetitDev 11y ago+1 for generators. As a former PHP developer, I jumped into using generators that return promises in io.js (as well as es6 classes), and the code reads very much like PHP code, except all of the i/o is now asynchronous.
- esailija 11y agoThere is no benefit of io being async in itself until you have many users. The immediate and more accessible benefit is speeding up individual requests due to the ease at which you can perfom io in parallel. But if you just sprinkle await/yield everywhere (which everyone unfortunately does), you don't even get this benefit.
- brentburgoyne 11y agoSure you can. let [a, b] = await Promise.all([asyncA(), asyncB()])
- esailija 11y agoI am referring to usage of generators without promises (or rather code that uses generators in a way that it wouldnt matter if promises or thunks were used). And even then I didn't say that you couldn't, even when using promises and generators together most people make their code unneceasarily sequential.
- kellros 11y agoThere was a post on HN recently on correctly using promises. A must read for anyone getting started with promises: http://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html http://pouchdb.com/2015/05/18/we-have-a-problem-with-promise...