Lame Old ES6

arrow funcs, desctructuring etc etc.

How can we use it in v10

Lets get out of .then spaghetti

async await


const InstantiateBeefBurger = () => {
    let calories;
    GetBuns()
    .then((buns) => GetSauces(buns.type))
    .then(() => GetPatties())
    .then((c) => {
        calories = c;
        Eat(calories)
    })
    .then(() => GetFatEventually(calories))
}



async const InstantiateBeefBurger = () => {
    const buns = await GetBuns()
    await GetSauces(buns.type)
    const calories = await GetPatties()
    await Eat(calories)
    await GetFatEventually(calories)
}

Readability, Less variable Declaration +1

FAQ: I don't want to change the entire method.  I just need to change a few things in the method.

I can't make the entire method async/ await and rewrite all the code, right?


function Test() {

    JS()
    .then(() => {})
    .then(() => {})
    .then(() => {})
    .then(() => {    
        //The part where you wanna change the code. 
        //You need to add more stuff in here.
                
    }).then(() => { ... }) //Add More
      .then(() => { ... }) //Add More
}
function Test() {

    JS()
    .then(() => {})
    .then(() => {})
    .then(() => {})
    .then(async () => {
    
        await Something() //Add more
        await Something() //Add more
        
    })
}
.then
async

Parallel Mindset With Async

Resolve Promises together. Not after 

.then().then().then()

const payload = {};

return GetCase()
.then(Case => {
    {...} //Some independent important code
    payload.Case = Case; //Append to payload
    return GetPatientByCaseId(Case.ID)

}).then(Patient => {
    {...} //Some independent important code
    payload.Patient = Patient //Append to payload, Dependent On CaseID
    return GetProviderByCaseId(Case.ID)

}).then(Provider => {
    {...} //Some independent important code
    payload.Provider = Provider //Append to payload, Dependent On CaseID
    return GetAppointmentByCaseID(Case.ID)

}).then(Appointment => {
    {...} //Some independent important code
    payload.Appointment = Appointment; //Append to payload, Dependent On CaseID
    return Mappers.DocumentReference(payload);
})

Problem: Appointment awaits for Provider to get resolved and Provider waits for patient to get resolved.

Readability also gets affected

const payload = {};

const Case = await GetCase();
payload.Case = Case; //Append to payload
 
const P1 = async function () {    
   {...} //Some independent important code
   payload.Patient = await GetPatientByCaseId(Case.ID)
}
const P2 = async function() {
   {...} //Some independent important code
   payload.Provider = await GetProviderByCaseId(Case.ID)
}
const P3 = async function() {
   {...} //Some independent important code
   payload.Appointment = await GetAppointmentByCaseID(Case.ID)
}

await Promise.all([P1(), P2(), P3()])
return Mappers.DocumentReference(payload);

Destructuring

Old School

let dependencies = [
    new emr_helper(this.context).getBase64Ccd(case_details)
];

if (case_details.user_id) {
    dependencies.push(new                
               lib.entity.external_system_users(this.context)
                    .getExternalSystemUserByInternalID(
                        case_details.user_id
                ));
        }
//===========================================================
const [base64ccd, provider] = await Promise.all(dependencies);
//===========================================================
let base64ccd, provider; 
const d = await Promise.all(dependencies)
.then((d) => {
    base64ccd = d[0]
    provider = d[1]
});

Cooler way



//Holds all the memory for everything in lib
const { entity, util, constants } = require('../../../../lib');
const { url }  = (await GetDownloadURL() || {}); 


//Holds all the memory for everything in lib
const lib = require('../../../../lib');

GetDownloadURL()
    .then(object => {
        object = object || {};
        const url = object.url;
    });

Old School

Cooler way

Made with Slides.com