Posts

Showing posts from September, 2023

Promises and closures 😢‍🌫️

 A simple Promise const   getData  =  new   Promise (( resolve ,  reject )  =>  {    reject ( 'error' );    resolve ( 'data' ); }); getData . then (( res ,  err )  =>   console . log ( res )); getData . catch (( res ,  err )  =>   console . log ( res )); A promise can have resolve or reject Same way when you read the response, you can have resolve or reject. You can read them both or you can have a separate catch function. In the above example, we get "error" instead of "data" as it was rejected 1st const   getData  =  new   Promise (( resolve ,  reject )  =>  {    resolve ( 'data' );    reject ( 'error' ); }); getData . then (( res ,  err )  =>   console . log ( res )); getData . catch (( res ,  err )  =>   console . log ( res )); This will give "data" and not "error" Static Functions of Promise ...