// //----Bad way: Don't use CallBacks------------------------------------------
//
//
// // Go to 01 below
//
// // 02 - download the file:
// // ========================
// function downloadFile(url,downloaded) {
// console.log(`Starting the Download from ${url}`);
// setTimeout(() => {
// const path = url.split('/').pop();
// downloaded(path); //Now go to 03 below
// },3000)
// }
// // 03 - perform the file compression
// // ==================================
// function compressFile(path, compressed) {
// console.log(`Starting the file compression for ${path}`);
// setTimeout(() => {
// const compressedPath = path.split('.')[0] + '.zip';
// compressed(compressedPath); // Go to 05 below
// },3000)
// }
// // 06 - upload the file:
// // ======================
// function uploadFile(compressedPath, uploaded) {
// console.log(`Starting to upload the file from ${compressedPath}`);
// setTimeout(() => {
// const uploadedPath = `http://localsystem/${compressedPath}`;
// uploaded(uploadedPath); // Go to 07 below
// },2000)
// }
// // 01-downloadFile function is called, passed args: (url and downloaded function)
// // =================================================================================
// downloadFile('http://facebook.com/profile.jpg', function (path) { //now, go to 02 above
// // 03 - downloaded(path) below is called:
// // ======================================
// console.log(`File Downloaded SuccesFully as ${path}`);
// compressFile(path, function (compressedPath) { //go to 04 above
// // 05 - compressed(compressedPath) below is called:
// // =================================================
// console.log(`File compressed as ${compressedPath}`);
// uploadFile(compressedPath, function (uploadedPath) { // go to 06
// // 07 - uploaded(uploadedPath) below is called:
// // ============================================
// console.log(`File uploaded successfully at ${uploadedPath}`);
// console.log("Everything's Done");
// })
// })
// });
// //-------------------------------------------------------------------------------------------------------
// -------------------------Good Way Using Promise---------------->
// // 02 - downloadFile(url) is executed below:
// // ==========================================
function downloadFile(url) {
console.log(`Starting the Download from ${url}`);
return new Promise(function (resolve, reject) {
if (!url.startsWith('http')) {
throw new Error("Something went wrong");
}
setTimeout(() => {
const path = url.split('/').pop();
resolve(path);// Go to 03 below
},3000)
})
}
// // 04 - perform the file compression:
// // ==================================
function compressFile(path) {
console.log(`Starting the file compression for ${path}`);
return new Promise(function (resolve, reject) {
setTimeout(() => {
const compressedPath = path.split('.')[0] + '.zip';
resolve(compressedPath); // go to 05 below
},3000)
})
}
// // 06 - upload the file:
// // =====================
function uploadFile(compressedPath) {
console.log(`Starting to upload the file from ${compressedPath}`);
return new Promise(function (resolve,reject) {
setTimeout(() => {
const uploadedPath = `http://localsystem/${compressedPath}`;
resolve(uploadedPath); // go to 07 below
},2000)
})
}
// // 01 - downloadFile(url) function is called below,
// // now go to downloadFile method definition at 02:
// // =================================================
downloadFile('https://facebook.com/profile.jpg')
// // 03 - compressFile(path) is called below:
// // =========================================
.then(compressFile) // go to 04 above
// // 05 - uploadFile(compressedPath) is called below:
// // ======================================
.then(uploadFile) // go to 06 above
// // 07 - Anonymous function(uploadedPath) is called below:
// // ======================================================
.then(function (uploadedPath) {
console.log(`File uploaded Successfully as ${uploadedPath}`);
console.log(`Everything Done`);
})
.catch(function (e) {
console.log(e.message);
console.log("Error");
})