0% found this document useful (0 votes)
82 views

Javascript Promise Simplified: Revealing The Concept Behind JS Promises

Promises provide a simpler way to handle asynchronous code compared to callbacks. They allow code to be written sequentially in a chain rather than nested. Promises have different states like pending, fulfilled, and rejected that help track asynchronous execution without nested callbacks. The 'then' method returns a new promise and takes callback functions as parameters to handle each state, resulting in cleaner code compared to deeply nested callback functions.

Uploaded by

Abhimanyu Singh
Copyright
© © All Rights Reserved
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Javascript Promise Simplified: Revealing The Concept Behind JS Promises

Promises provide a simpler way to handle asynchronous code compared to callbacks. They allow code to be written sequentially in a chain rather than nested. Promises have different states like pending, fulfilled, and rejected that help track asynchronous execution without nested callbacks. The 'then' method returns a new promise and takes callback functions as parameters to handle each state, resulting in cleaner code compared to deeply nested callback functions.

Uploaded by

Abhimanyu Singh
Copyright
© © All Rights Reserved
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
You are on page 1/ 9

Javascript Promise Simplified

Revealing the Concept Behind JS


Promises.

By ABHIMAN!

AS" o#rself $$$$

%hat is Promise $

Is it Replacement for
Call&ac' $

If es ( %hy $

If Not( %hy $

In my )ords

Promises is a )ay to implement call&ac'

Hides the comple*ity of call&ac' and simplify o#r


code

Provides maintaina&lity
You could create your own promise
Let us begin

Callback chain results into 'Spaghetti code'
Chain of Callbacks in the code frustrate us
doSomething(callback){
var value !"#
callback(!")#
$
callback(anotherCallback){
var value %got frustrated of this chain&
anotheCallback(value)
$


Promise )ay to do
+oSomething,-.then,callBac'
,--.then,anotherCallBac',--

AS" .!RS/01 $$
ho) Promise does the
e*ec#tion $
%hat is 2then2 $


S/CR/3 R/4/A0/+

2then2 ret#rns promise.

/*ec#tion
doSomething,-5
66yo#r code
ret#rn5
then7 f#nction,call&ac'-5
var val#e 8 9:;
call&ac',9:-
<
<
<
call&ac',val#e-566yo#r code<
'eally ( ')hen' returns promises *

2then2
3hen accepts t)o f#nction

Resolve,-

Re=ect,-
%hy t)o
f#nctions $
Is it necessary $

PR.MIS/ Have State
S)+),S
resolve
reject
pending
%hy these states $
-on't need to think
Look into previous slide

Still )hinking ( .hy '/ending' State *
0/3 !S 0.." .N 3HIS 0IN" 1.R 1!00 PR.MIS/ C.+/
https766docs.google.com6a6nisostech.com6doc#ment6d6>*4e?@::e?'A@eaPMvA
NoBsoCCI)?rJ.RS"IfBtD0?Bc6edit
E

You might also like