Closed Bug 805422 Opened 7 years ago Closed 7 years ago

Use promises to avoid the callback spaghetti

Categories

(L20n :: JS Library, defect, P3)

x86_64
Linux
defect

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: stas, Unassigned)

References

Details

To quote the author of the Q library:

"[p]romises can mitigate the “Pyramid of Doom”: the situation where code marches to the right faster than it marches forward.

step1(function (value1) {
    step2(value1, function(value2) {
        step3(value2, function(value3) {
            step4(value3, function(value4) {
                // Do something with value4
            });
        });
    });
});

With a promise library, you can flatten the pyramid.

Q.fcall(step1)
.then(step2)
.then(step3)
.then(step4)
.then(function (value4) {
    // Do something with value4
}, function (error) {
    // Handle any error from step1 through step4
})
.end();"

See http://wiki.commonjs.org/wiki/Promises/A.

There are a lot of libraries that implement the spec, but these to come up the most:

https://github.com/kriskowal/q
https://github.com/tildeio/rsvp.js

https://gist.github.com/3889970 is also a good read (by the author of the spec and the co-author of Q)
Priority: -- → P3
Target Milestone: --- → Next
This landed as part of the ctx2 branch.
Status: NEW → RESOLVED
Closed: 7 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.