0

I am trying to use a Promise function. The promise function is nested inside the dataService function. And the dataService function has a getData method inside, it defines a promise and then checks , if the number inside generated randomly is greater than 5 then the promise variable executes resolve else its a reject (inbuilt promise parameters). The then property logs the answer. However i am getting errors like - Cannot read property 'then' of undefined and Cannot read property 'resolve' of undefined at Promise.html:19. Please help.

 <html>

 <head>
<title>Promise</title>
</head>

<body>
<script>
   function dataService() {
        var obj = {};
        console.log('inside function');

        obj.getData = function () {
            var promise = new Promise(function(resolve,reject){

                var number = Math.random() * 10;

                if (number > 5) {
                   ( promise.resolve("Resolved promise " + number));

                }
                else
              ( promise.reject("Promise Rejected " + number));

        })};

            return obj;

        }


    var data = dataService();
    console.log(data);

   data.getData()
        .then(function(val) {
            console.log("It has been a success"),
            console.log(val)
         } )
        .catch(function (err) {
                console.log("It has been failed"),
                console.log(err)
        });
  </script>

 </body>

 </html>

3 Answers 3

1

You should return the Promise when the function is called:

function dataService() {
        var obj = {};
        console.log('inside function');

        obj.getData = function () {
            return new Promise(function(resolve,reject){

                var number = Math.random() * 10;

                if (number > 5) {
                   ( resolve("Resolved promise " + number));

                }
                else
              ( reject("Promise Rejected " + number));

        })};

            return obj;

        }


    var data = dataService();
    console.log(data);

   data.getData()
        .then(function(val) {
            console.log("It has been a success"),
            console.log(val)
         } )
        .catch(function (err) {
                console.log("It has been failed"),
                console.log(err)
        });

Sign up to request clarification or add additional context in comments.

Comments

0

Don't use promise.resolve and promise.reject. Just use resolve and reject.

Also, you need to actually return the promise from your getData function.

Try educating yourself with a tutorial, e.g. this one: https://developers.google.com/web/fundamentals/primers/promises

1 Comment

still getting errors --> Cannot read property 'then' of undefined. And --> Uncaught (in promise) Promise Rejected 2.0840987564690128
0

You are returning with an object instead of a promise object

obj.getData = function () {
    var promise = new Promise(function(resolve,reject){
    // omitted
    return obj; // <---- culprit.
}

just return with promise instead

obj.getData = function () {
    var promise = new Promise(function(resolve,reject){
    // omitted
    return promise; // <---- changed
}

so that when doing data.getData(), you will receive a Promise object. This object has .then and .catch functions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.