DEV Community

subash
subash

Posted on

#PROMISE

  const promise = new Promise((res,rej) =>{
            rej()
        });

        promise
          .then(()=>{
            console.log("done")
          })
          .catch(()=>{

            console.log("error")
          })
Enter fullscreen mode Exit fullscreen mode

In this case we have promise in promise variable, new is key word, default promise have two aruguments which is resolve and reject. this case we call res() so then it will go to then , if we call rej() then it will go catch.

2.



        function Analysis(){
            const analysisPromise = new Promise((res,rej)=>{
                setTimeout(()=>{
                    console.log("analysis completed")
                    res()
                },4000)
            })
            return analysisPromise;
        }
        function Design(){
            const designPromise = new Promise((res,rej)=>{
                setTimeout(()=>{
                    console.log("design completed")
                     res()
                },9000)
            })
            return designPromise;
        }
        function Development(){
            const developmentPromise = new Promise((res,rej)=>{
                setTimeout(()=>{
                    console.log("Development completed")
                     res()
                },4000)
            })
            return developmentPromise;
        }
        function Testing(){
            const testingPromise = new Promise((res,rej)=>{
                setTimeout(()=>{
                    console.log("Testing completed")
                     res()
                },4000)
            })
            return testingPromise;
        }
        function Deployment(){
            const deploymentPromise = new Promise((res,rej)=>{
                setTimeout(()=>{
                    console.log("Deployment completed")
                     rej()
                },4000)
            })
            return deploymentPromise;
        }



        Analysis()

             .then(() => Design())
             .then(() => Development())
             .then(() => Testing())
             .then(() => Deployment())
             .catch(() => console.log("error"))
Enter fullscreen mode Exit fullscreen mode

First we created function, inside that function have promise, inside that promise have setTimout, inside that setTimeout we gave console.log and res(). this function is return that promise.
we have diff function with diff names.each settimout have 4sec gap..

after we callback first functions first one is Analysis() , this function return analysisPromise; setTimout have 4second waiting time and also print analysis completed the gave res(). we use res() then it will going to "then" after that work what inside that then , so we should create then catch syntax.in analysis res() will go to first then that have Design callback , that will return designpromise , it will go like this series upto deployment. if anything rej in between them , after that catch will work.

Top comments (0)