In order to better understand Promise syntax, I'm trying to convert the following pseudo-code into modern JavaScript.
Here's the old code, featuring the infamous callback hell:
1995 JavaScript
var floppy = require( 'floppy' );
floppy.load( 'disk1', function( data1 ) {
floppy.prompt( ' Please insert disk 2', function() {
floppy.load( 'disk2', function( data2 ) {
floppy.prompt( ' Please insert disk 3', function() {
floppy.load( 'disk3', function( data3 ) {
floppy.prompt( 'Please insert disk 4', function() {
// If Node.js would've existed in 1995.
} );
} );
} );
} );
} );
} );
Please help me convert the above into JavaScript with Promise syntax (no async/await). I'm a Promise newbie, but here's what I came up with. Is the syntax, nesting and functionality the same as the above code? If not, please help me convert the callback madness into nice JavaScript with Promises.
Updated Code
const floppy = require( 'floppy' );
floppy.load( 'disk1' )
.then( data1 => {
floppy.prompt( 'Please insert disk 2' );
} )
.then(
floppy.load( 'disk2' )
)
.then( data2 => {
floppy.prompt( 'Please insert disk 3' );
} )
.then(
floppy.load( 'disk3' )
)
.then( data3 => {
floppy.prompt( 'Please insert disk 4' );
} )
.then(
// Node.js using Promises avoids callback hell!
);