Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Introduction to JavaScript Promises and Lax in Lightning
1. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE
As c on Ja c t i L h n
Com n us S C ax
28 JU
t
SA , 2018
1
Sikha Baid
baid.sikha@gmail.com
@sikhabaid
2. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE
I am S a d
Sr. Salesforce Developer
Cognizant Technology Solutions
Twitter: @sikhabaid
Hello!
2
4. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE 4
Wha ’s i r
● The Problem
● Handy ES6 or EcmaScript 6 Features
● What’s all the buzz about JS Promises
● The Callback Hell
● Welcome – SFDC Lax powered by JS Promises
● Let’s write some code using Lax
● Things to Remember
5. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE
The l – Wha
ar ol g
5
● Need to write elegant JavaScript in LC
● JavaScript is a first class citizen like Apex
● Solving the Traditional Callback Hell Problem
6. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE 6
Han ES6 or E Sc t 6 Fe t e
● Are you still using the age old JS ?!?
● Have you explored the new JS features –
❏ Let/Const Keywords
❏ Arrow Functions
❏ Promises
❏ And much MOAR……
7. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE 7
ES6 – Let/Con
Let - declare variable in limited scope
Const - Constants which cannot be assigned again, it’s fixed.
Let’s e t co !
8. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE 8
ES6 – Ar o F n on
Functions syntax has changed a bit with ES6.
1. Declare and assign variable as a function.
2. Body of the function.
Let’s e t co !
9. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE 9
The b Hel
It consists of multiple nested callbacks
which makes code hard to read,
debug and maintain.
It is understandable how one
might unknowingly get caught in
Callback Hell while dealing with
asynchronous logic.
Let’s e t co !
10. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE
● It ain’t any new feature.
● A syntactic sugar to write async. JS more
elegantly
● Write more readable and intuitive JS than
ever!
● Could be a little overwhelming but trust me
will be easier soon ;-)
Wha ’s a t z a t J o s
10
Let’s e t co !
11. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE
What if you could write Async JS in
Lightning avoiding the complexities
of Promises ?!?
Wel – S C ax re y
J ro s
11
12. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE
● Write a clear asynchronous JavaScript code
● Efficiently call Apex with powerful exception
handling
● Ton of utility techniques
● Start writing JS in the modern approach
Wh D ax ?
12
14. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE 14
2 3
Set g La
Deploy using either of the the listed
1. Hit the url
https://githubsfdeploy.herokuapp.com/app/githubdeploy/ruslan-k
urchenko/sfdc-lax and hit deploy.
2. Visit the github repo mentioned in the references and deploy using
the ‘Deploy to Salesforce’ button.
16. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE 16
Create an account
with details entered
on UI
Create a contact
record linking it to
created contact
Send an email
notification to the
email address
confirming the account
creation.
Resolve
Resolve
Show error/exception
on UI
Reject
17. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE 17
2 3
Wha n S C ax ?
● Performing multiple chained actions
lax.enqueue(name[, params[,
options]]).then(callback).then(callback)
● Performing multiple concurrent actions
lax.enqueueAll(actions).then(callback)
● Create and return LaxAction builder
lax.action(name)
18. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE 18
2 3
Wha n S C ax ?
● function to load a record template to the LDS targetRecord
attribute
lax.lds(id).getNewRecord(sobjectType[,
recordTypeId[, skipCache]])
● the function to save the record that loaded to LDS in the edit EDIT
mode
lax.lds(id).saveRecord()
● the function to delete a record using LDS
lax.lds(id).deleteRecord()
19. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE 19
2 3
Wha n S C ax ?
● creates an object with LaxEventBuilder prototype and the context
event by provided name
lax.event(eventName)
● creates a component(s) from a type and a set of attributes
lax.createComponent(type[, attributes])
20. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE 20
● SFDC Lax is just a service Lightning
Component to welcome you to the world
of Promises
● You can build your own Promise based
Components too!
● It’s never too late and you can still write
better JS!
● Taking a moment to thank the SFDC Lax
creator - Ruslan Kurchenko
Thi s R em
21. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE 21
Ref ce
● ES6 - https://hackernoon.com/es6-for-beginners-f98120b57414
● Promises - https://scotch.io/tutorials/javascript-promises-for-dummies
● Lax - https://github.com/ruslan-kurchenko/sfdc-lax
● API Reference - https://ruslan-kurchenko.github.io/sfdc-lax/
● Execute anonymous JS -
https://es6console.com/
https://repl.it/repls/TrimCleanLine
23. www.jaipurdevfest.com @jaipurdevfest #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTEwww.jaipurdevfest.com @jaipurdevfest jaipurdevfest@googlegroups.com #JDF18 - CONFIDENTIAL | DO NOT DISTRIBUTE 23
ThanksYou can find me at @sikhabaid on twitter or email : baid.sikha@gmail..com