0

I am trying to understand Promises and how to write them.

const getmsg = ( ) =>{
    setTimeout(()=>{
        console.log('Delaying this Message');
    },3000);
}

const displaymsg=()=>{
    console.log('Displayed after getmsg() is executed')
}

const promise = new Promise((resolve,reject)=>{
    resolve(getmsg)
})

promise
    .then(response =>{
        displaymsg();
        console.log(response)
        })
    .catch(err =>{
        console.log(err)
        })

I have written two functions getmsg() and displaymsg(). I want the displaymsg() to be executed after getmsg(). According to my understanding i am creating a promise which executes getmsg and then displaymsg() is getting called. But it isn't happening here. Where am i doing wrong.

  • You are executing displaymsg() right when your promise is resolved i.e., in .then({}) – Beshambher Chaukhwan Feb 15 '21 at 03:44
  • if you check you log you'll see that `response` contains the function `getmsg`. At no point in your code are you actually **executing** `getmsg`, you're just passing it around. – Thomas Feb 15 '21 at 03:45
  • The resolve statement in your promise does nothing but triggers the completion of your promise statement with the data u provide in resolve() in here you are giving it a method signature of getmsg or simply it will take it as undefined if its not defined above. You need to call getmsg() right before displaymsg() inside your promis.then({}) callback – Beshambher Chaukhwan Feb 15 '21 at 03:46
  • 1
    @BeshambherChaukhwan [repl](https://repl.it/@TusharRoy/StandardTriflingPresses#index.js) Please let me know how you would do it on this repl. –  Feb 15 '21 at 04:00
  • const promise = new Promise((resolve,reject)=>{ resolve(true); // u can pass data here }) promise .then(response => { getmsg(); displaymsg(); console.log(response) }) .catch(err =>{ console.log(err) }) – Beshambher Chaukhwan Feb 15 '21 at 04:03
  • I'm unable to format it can you edit this comment – Beshambher Chaukhwan Feb 15 '21 at 04:06
  • @BeshambherChaukhwan thank you for your response but it first executes displaymsg and then getmsg after a delay. try this [repl](https://repl.it/join/ioeshcfk-tusharroy) –  Feb 15 '21 at 04:17
  • That's because there is a set timeout function inside getmsg() – Beshambher Chaukhwan Feb 15 '21 at 04:28
  • Check now I edited the code – Beshambher Chaukhwan Feb 15 '21 at 04:30
  • Yeah so i am delaying that intentionally so that getmsg takes time and then it runs after which displaymsg should run. For ex: If i make an api call then it will take time but i want the display function to run only after the api call is finished. That is why we use callbacks and promises. If i'm not wrong. –  Feb 15 '21 at 04:32
  • Yeah. Add delay before resolve() while creating promise if you are mocking the api call delay. – Beshambher Chaukhwan Feb 15 '21 at 04:33
  • Resolve will only hit after the api call is done be it a 200 success or any other failure – Beshambher Chaukhwan Feb 15 '21 at 04:34
  • I am really confused about creating promises. Please let me know where i understood wrong. Accroding to me we create callbacks so that it does not gets executed at last in a callback. Like if we have getmsg and displaymsg then we want to call getmsg(callback){//inside here we get the msg and then call the callback ryt? –  Feb 15 '21 at 04:46

0 Answers0