today, we'regonnabelookingathowitcanuse a sinkawaittomakeJohnMuskerpromiseseasiertoworkwithbeforewediveintowhatthiscodeactuallydoes.
Ifyoudon't alreadyunderstandhowpromiseswork, makesureyoucheckoutmyJavaScriptpromisesvideolinkedinthecardsandthedescriptionbelowbeforeyoucomebacktothisvideo, becauseyouneedtounderstandpromisesbeforeyoucanunderstandhowtouse a sinkand a weightsince a sinkand a weightisreallyjustsent, tacticalsugarwrappedaround, makingpromiseseasiertoworkwith.
Solet's lookatthecodethatwealreadyhaveinplace.
Wehavetwofunctionshere.
Thefirstfunctionmakerequestreturns a promisethatwe'lljustfogmaking a request.
Andthenit'llresolvesuccessfullyifyousendin a locationofGoogleorit'llrejectifyoudonot.
AndthenoursecondfunctionhereProcessrequestis a verysimplerequestthatjustadds a littlebitofextrainformationontothestringthatyoupassintoitandit'llalwaysresolve.
Solet's lookathowwewouldcallthesefunctionsusingpromisesandthenwe'regoingtogothroughandtakethatcoatandchangestouse a sink, a waitandseealloftheadvantagesanddisadvantagesofdoingso.
Sowhat's first?
Rideitout.
I justwanttocallthemakerequestfunction, andwe'regoingtopassitandGoogle, forexample, sinceweknowthatthatwillmakeitsucceed, andthenwecouldjustusedotThen, sincewe'reusingthepromiseherewehaveourresponsetothatisgoingtobereturnedbyourpromise, whichisjustinourcase, a stringhere.
So, forexample, ifwetriedtoqueryFacebookinsteadofGoogleandwesaythatyousaymaking a requestofFacebookandthenwegetanairsayingwecanonlytalktoGoogleandthat's exactlywhatwewant.
Andifyoulookatthiscode, it's reallynottoobad, butwecanmakeit a loteasiertounderstandwithouthavingallthesedotbendsandthenestinginsideofthem.
Andif, forexample, weneededtouse a parameterinsideofthisfirstdotBeninsideofourseconddotBen, wecan't dothatveryeasily.
Promiseswehavetocreate a variableouthere, callitsomething, andwewouldhavetoassignsomething, too.
Butwith a singleweight, wedon't actuallyneedtoworryaboutthat.
Sodownherebelowthis, make a request.
Let's dothis a circuitouslywith a singleweight.
Thefirstthingyouneedtoknowabout a sinkawayisthatyouneedtohavesomekindoffunctionthatyou'reawaitingcodeisinsideof, Soyou'regonnacreate a function.
Andthenlastly, allweneedtodoistakeourfinalconcertwogputitinthebottomherebecausenowweknowassoonasthisprocessrequestisfinishedwaiting, it'llcomedownhereand a longprocessresponse.
Thebiggestthingstomakesurethatyoudowhenyou'reusing a sinkawaitisthatyoumustwrapyourcodeinsideof a function, whetherit's ananonymousfunctionandaerofunctionornormalfunctionlikethis.