arrow funcs, desctructuring etc etc.
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)
}
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
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);
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]
});
//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;
});