Firstoff, allwillgothrough a briefhistoryoffchasingprogramminginJavascript.
We'llseehowitevolvesovertime.
We'llquicklytake a lookatthecallbacks, promisesgeneratorsandfinally a sinkaway.
Solet's getstarted.
Okay, so I alreadymentionedthis, butthisishistoryfacingprogramming, jobsstripped.
Soitallstartedbackwithcallbacksinthepast, andthenwewereabletousepromises, whichwas a majorlikeshiftfromcallbacks.
Andthenwewerealsoabletoitsgenerators.
Now I didn't usegeneratorsmuch, prettymuchlikeskippedoverthem.
So I wentstraightfrompromisesto a singleweight.
Butgeneratorsareinbetweenthosetwostagesandfinally, like, rightnowinthepresenttime, wehaveaccesstothesehonestly, likeamazingfeaturethatiscalled a sinkaway.
Sowhattheywanttodonextisthatwewanttopresenttoyou a pieceofcode, andthen I wanttoshowyouthesamepieceofcodein C callbacks, promisesand a thinkingthewayforthegenerators.
I honestlywouldn't wanttoshowthatbecause I personallydon't youhave a lotofexperienceusinggeneratorsside.
I don't wanttoshowyousomething.
Dates.
Maybe I badlywritten.
So I'm goingtoskipoverthegenerators.
Soletmeshowyouthat.
Andhereitis.
Sothisisthecallbackexam.
Now.
I reallydidn't wanttocreate, like, themostcomplexcallbackexampleever.
Sowhatwehavehereis a functiondateissupposedtoebeandendpointChandler.
So, forexample, let's sayyouwerebuildingsomesortof a website, andthescandaloverherewillreceiverequestsfromtheclient, forexample, thebrowsaresendingrequeststotheserver, andthisresponseobjectiswhatyoucanusetoactuallywritebacksomethingtotheclient.
Forexample, towtheuserbrowsingyourwebsites.
Sonowwhatwehaveinthisexampleiswe'retalkingwiththedatabaseandlet's saywehave a tableor a collectionforuser's, andwewanttofind a particularuserthatwillbesenttousviathisrequest.
Sowewanttocallthisfunctionuserthatfindby i d.
AndsimplybestThisrequirestheuser i d property.
Butnowdysfunctionisfacingone.
Wedon't knowhowmanymillisecondsitisgoingtoactuallyrunfor, soweneedtodothisin a sinkmannerbecauseotherwisewe'regoingtoblockthecoat.
Andyou'realsoabletounderstand, like, whyisthere a needfortheschoolbackfunctions?
Andnowlet's take a lookhowallofthese, liketheexactsamecourtissuehandledwhenitcomestothepromisesandthisis a promise, isexample, andonethingyoucanspotstraightawayeasilyhow, howmuchclearandcleanerthiscouldactuallyis.
Twokeywordsinsideofthatfunctionthatisdeclaredtobe a racingfunctiontheythinkawaitisactuallyallpromisesunderthehood.
So, likeifyoureallywanttogetahdeepanddetailedunderstandingoff a sinkawaitfeature, it's bestifyoulearnpromisesfirst.
But I'lldomybestatleastexplainyouwhat's goingonoverhere.
Sofirstthingyoucannoticeiswehavethistrycatchconstruct, andthatishowyou'reabletohandleerrorsin a sink, awaitsfunctionsSothesketchpieceoverhereissimilartothecatchbeastswehaveseenoveratthepromisesExample.
Andnowthisnextlinetasksequalsawaituswouldfindby a D willonlyexecuteaftertheuserequalsof 80 residentsfindby a d getsresolvedSoyoucanalmost, like, readitlike a sinkblockingquote.
Sonextwewaitforthetasksandthenwesupplythisuserthetasks i d.
So I hopethatnowyoucanappreciatetheabilitiesthat I think a greatfeatureactuallygivesusaswedoourdailyJavascriptchasingprogramming.
Sowhat I wanttodonextis I don't wanttostophere, butinstead I wanttogowithyouthrough a bunchof a thinkawaitexampleswin, which I couldshowyouthesequentialflowbarrelflow.
I alsowanttoshowyoulikealltheloopsanddeteriorationthatyoucandowith a sink.
A Wait, I'm goingtoshowyousomecommongorgeousthatyoumightbeunawareoffatthismoment.
Andallinall, I justwanttoletgothroughit, A bunchofexamples.
Sodidyoufeelconfidenceinyourabilitytostartusingthis A sinkawayfeaturetoday?
Okay, sonowlet's dolike a bunchofexamplessothatyoucanseealltheinsandoutsoffusing a sinkawaitingyourcode.
We'reactuallyusingthispromiseif I functionfromtheutilpackageinnoJezwhichwillactuallyturnthesettimeoutintow, a promisebasedfunctionsthatwecanuseinsideofour 18 coats.
Soremember, likethe A sinkisprettymuchlikepromiseswithahdifferentinterfacethatwecaninteractwithsowellwecan't reallyusecallbackfunctionsinsideofourracingfunction.
Allofthatneedstobepromises.
That's thereasonbehindthis.
And I didn't tellthisbefore, But I'm usingnoteforthisbecauseit's mucheasierformetoeusinggold.
Sortof, um, thingslikethesedidalreadybuiltin.
Butyoucan, ofcourse, learn a singleweightanduseitfor, likeeverythingthatyoudoinsideofJavaScript.
Sowiththesethreeexamples, youshouldfeelmoreandmoreconfidenceinwriting a think, obviouslythemoreyoulearn, andthemoreyouknowaboutpromises, it's goingtobenefityou a lot.
Soevenifyou'reinterested, sinkawayexclusively.
I wouldsuggestthatyou, like, takesometimeandgooverpromisesinyoursparetime.
Butnowlet's take a lookatwhathappenswhenwewanttoexecutethingsusingthesinkawaitingparallel.
Andlet's seehowdeadeffectsarepromisedthatallfunctionnow, yourememberwhatyoujustsaidisthatthepromise, a tollreturns a singlepromise, andthatsinglepromiseactuallyfailswiththereasonoffthefirstpromisethatfailsoverhere.
You'veseenthatifyouhavesomelegacycodeorsimplythemarchthatyou'reusingisimplementingthecallbackinterface, meaningitisexpectingyoutopassing a callbackfunction.
Youcan't reallyuseitdirectlyinyour I thinkourfunction, butwhatyoucandoEasternDeadfunctionintow, a promise, andthenyoucanuseitwith a waytokeywords.
Sopromiseisreallyimportantthere.
Whatallowedthecasingtoactuallyfunction?
Sobesureto, like, studyonpromises.
Then I briefedmansiongenerators.
Now I don't reallycaremuchexperienceusinggenerators, so I didn't wanttorushinandlikecreatesomeexamplesbecause I don't thinkit's necessary, giventhatwehaveaccesstoourtow a sink, a weightnowadaysandthenfinallywe'veseenaboutsinkawaits.