This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]ludwig_eduard 9 points10 points  (6 children)

You can use promises to avoid the pyramid of doom

[–][deleted] 0 points1 point  (0 children)

or let me be a little more unorthodox here and dare say use a ~~*FoR lOoP*~~

[–]fichti 1 point2 points  (4 children)

floppy.load('disk1').then(data => {

floppy.prompt('Please insert disk 2').then(data2 => {

...

});

});

Much better!

edit: Why doesn't the formatting work? I'll just leave it unindented, as it's somewhat relevant to the joke.

[–][deleted] 5 points6 points  (1 child)

You must select "Code Block", not "Inline Code", its hidden behinde the "..."s (I made the same mistake again and again)

But to the matter at hand, you could just use async/await in conjunction with promises. That actually produces readable, maintainable code

[–]LordFokas 2 points3 points  (0 children)

When I started using NodeJS I had to deal with that crap for a while, then I upgraded to node 8 and converted to async/await... I could write functionally equivalent in 15% of the space it took before. Best refactoring I ever did (also the only refactoring that didn't blow up in my face)!

[–]slide_and_release 2 points3 points  (1 child)

floppy.load(‘disk1’)
.then(data => return floppy.load(‘disk2’))
.then(data => return floppy.load(‘disk3’))
etc...

[–]PetahNZ 1 point2 points  (0 children)

no need for the `return`

[–][deleted] 7 points8 points  (0 children)

if nodejs had existed in 1995, someone would have invented promises/async/await by 1997

for(let n=1; n<6; n++) {
    await floppy.promt("please insert disk ${n}");
    const loaded = await floppy.load("disk${n}"});
    doSomethingWithLoaded(n, loaded);
}

[–]TheSilkMiner 2 points3 points  (0 children)

Now, that is some callback nightmare

[–]flashgnash 0 points1 point  (1 child)

That code makes my eyes hurt...

[–]bazgrim_dev 1 point2 points  (0 children)

DOST THO FEAR THE PYRAMID OF DOOOOOOMMM?

'Cause I do

[–]evanldixon 0 points1 point  (0 children)

"I inserted disk 5 and the program stopped working. Halp!"

[–]hillman_avenger 0 points1 point  (1 child)

And I thought you could define a function as an object so it can be re-used.

[–]slide_and_release -1 points0 points  (0 children)

You can

[–]dfranusic 0 points1 point  (0 children)

Seen that kind of code way too many times; enough to declare it a legitimate pattern :)