get deferred promise value jQuery












0















I have below function



function commentCount(id) {
var deferred = new $.Deferred()
var url =
_spPageContextInfo.siteAbsoluteUrl +
"/_api/lists/getByTitle('Comments')/items?$select=Id&$filter=ItemID eq " +
id +
" and Title eq 'Colleague'"
getData(url).then(function(data) {
deferred.resolve(data.d.results.length)
})
return deferred.promise()
}


Now I want to return that value in HTML string as below but it shows [object object]



'<div class="card-footer"> <div class="row "><div class="col-lg mobile-center"><a href="#">' + commentCount(item.Id)


How can I get value like that










share|improve this question























  • It is integer value

    – Milind
    Nov 23 '18 at 23:36






  • 1





    commentCount is going to return a promise,.. Using deferred doesn't change things here, you could have just done return getData(url).

    – Keith
    Nov 23 '18 at 23:36











  • Can you please show me example

    – Milind
    Nov 23 '18 at 23:38











  • Unless you say used async / await your can't do something like "some string" + somePromise(), you need to resolve your promise first. If using async / await, this could be done -> "some string" + await somePromise()

    – Keith
    Nov 23 '18 at 23:40













  • How we can do that

    – Milind
    Nov 23 '18 at 23:42
















0















I have below function



function commentCount(id) {
var deferred = new $.Deferred()
var url =
_spPageContextInfo.siteAbsoluteUrl +
"/_api/lists/getByTitle('Comments')/items?$select=Id&$filter=ItemID eq " +
id +
" and Title eq 'Colleague'"
getData(url).then(function(data) {
deferred.resolve(data.d.results.length)
})
return deferred.promise()
}


Now I want to return that value in HTML string as below but it shows [object object]



'<div class="card-footer"> <div class="row "><div class="col-lg mobile-center"><a href="#">' + commentCount(item.Id)


How can I get value like that










share|improve this question























  • It is integer value

    – Milind
    Nov 23 '18 at 23:36






  • 1





    commentCount is going to return a promise,.. Using deferred doesn't change things here, you could have just done return getData(url).

    – Keith
    Nov 23 '18 at 23:36











  • Can you please show me example

    – Milind
    Nov 23 '18 at 23:38











  • Unless you say used async / await your can't do something like "some string" + somePromise(), you need to resolve your promise first. If using async / await, this could be done -> "some string" + await somePromise()

    – Keith
    Nov 23 '18 at 23:40













  • How we can do that

    – Milind
    Nov 23 '18 at 23:42














0












0








0








I have below function



function commentCount(id) {
var deferred = new $.Deferred()
var url =
_spPageContextInfo.siteAbsoluteUrl +
"/_api/lists/getByTitle('Comments')/items?$select=Id&$filter=ItemID eq " +
id +
" and Title eq 'Colleague'"
getData(url).then(function(data) {
deferred.resolve(data.d.results.length)
})
return deferred.promise()
}


Now I want to return that value in HTML string as below but it shows [object object]



'<div class="card-footer"> <div class="row "><div class="col-lg mobile-center"><a href="#">' + commentCount(item.Id)


How can I get value like that










share|improve this question














I have below function



function commentCount(id) {
var deferred = new $.Deferred()
var url =
_spPageContextInfo.siteAbsoluteUrl +
"/_api/lists/getByTitle('Comments')/items?$select=Id&$filter=ItemID eq " +
id +
" and Title eq 'Colleague'"
getData(url).then(function(data) {
deferred.resolve(data.d.results.length)
})
return deferred.promise()
}


Now I want to return that value in HTML string as below but it shows [object object]



'<div class="card-footer"> <div class="row "><div class="col-lg mobile-center"><a href="#">' + commentCount(item.Id)


How can I get value like that







javascript jquery es6-promise jquery-deferred






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 23:31









MilindMilind

60021441




60021441













  • It is integer value

    – Milind
    Nov 23 '18 at 23:36






  • 1





    commentCount is going to return a promise,.. Using deferred doesn't change things here, you could have just done return getData(url).

    – Keith
    Nov 23 '18 at 23:36











  • Can you please show me example

    – Milind
    Nov 23 '18 at 23:38











  • Unless you say used async / await your can't do something like "some string" + somePromise(), you need to resolve your promise first. If using async / await, this could be done -> "some string" + await somePromise()

    – Keith
    Nov 23 '18 at 23:40













  • How we can do that

    – Milind
    Nov 23 '18 at 23:42



















  • It is integer value

    – Milind
    Nov 23 '18 at 23:36






  • 1





    commentCount is going to return a promise,.. Using deferred doesn't change things here, you could have just done return getData(url).

    – Keith
    Nov 23 '18 at 23:36











  • Can you please show me example

    – Milind
    Nov 23 '18 at 23:38











  • Unless you say used async / await your can't do something like "some string" + somePromise(), you need to resolve your promise first. If using async / await, this could be done -> "some string" + await somePromise()

    – Keith
    Nov 23 '18 at 23:40













  • How we can do that

    – Milind
    Nov 23 '18 at 23:42

















It is integer value

– Milind
Nov 23 '18 at 23:36





It is integer value

– Milind
Nov 23 '18 at 23:36




1




1





commentCount is going to return a promise,.. Using deferred doesn't change things here, you could have just done return getData(url).

– Keith
Nov 23 '18 at 23:36





commentCount is going to return a promise,.. Using deferred doesn't change things here, you could have just done return getData(url).

– Keith
Nov 23 '18 at 23:36













Can you please show me example

– Milind
Nov 23 '18 at 23:38





Can you please show me example

– Milind
Nov 23 '18 at 23:38













Unless you say used async / await your can't do something like "some string" + somePromise(), you need to resolve your promise first. If using async / await, this could be done -> "some string" + await somePromise()

– Keith
Nov 23 '18 at 23:40







Unless you say used async / await your can't do something like "some string" + somePromise(), you need to resolve your promise first. If using async / await, this could be done -> "some string" + await somePromise()

– Keith
Nov 23 '18 at 23:40















How we can do that

– Milind
Nov 23 '18 at 23:42





How we can do that

– Milind
Nov 23 '18 at 23:42












1 Answer
1






active

oldest

votes


















1














Your function is returning a promise and you have to resolve that promise before you can get at the data



It is also using a needless $.Deferred since getData() returns a promise



Needs to be something like:



function commentCount(id) {

var url ='....';

// return the getData promise
return getData(url).then(function(data) {
// return the count to next `then()` in chain
return data.d.results.length
})

}

commentCount(someId).then(function(count){
// do stuff with your html here
var str ='<div> Count:' + count + '</div>';
})





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453873%2fget-deferred-promise-value-jquery%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Your function is returning a promise and you have to resolve that promise before you can get at the data



    It is also using a needless $.Deferred since getData() returns a promise



    Needs to be something like:



    function commentCount(id) {

    var url ='....';

    // return the getData promise
    return getData(url).then(function(data) {
    // return the count to next `then()` in chain
    return data.d.results.length
    })

    }

    commentCount(someId).then(function(count){
    // do stuff with your html here
    var str ='<div> Count:' + count + '</div>';
    })





    share|improve this answer






























      1














      Your function is returning a promise and you have to resolve that promise before you can get at the data



      It is also using a needless $.Deferred since getData() returns a promise



      Needs to be something like:



      function commentCount(id) {

      var url ='....';

      // return the getData promise
      return getData(url).then(function(data) {
      // return the count to next `then()` in chain
      return data.d.results.length
      })

      }

      commentCount(someId).then(function(count){
      // do stuff with your html here
      var str ='<div> Count:' + count + '</div>';
      })





      share|improve this answer




























        1












        1








        1







        Your function is returning a promise and you have to resolve that promise before you can get at the data



        It is also using a needless $.Deferred since getData() returns a promise



        Needs to be something like:



        function commentCount(id) {

        var url ='....';

        // return the getData promise
        return getData(url).then(function(data) {
        // return the count to next `then()` in chain
        return data.d.results.length
        })

        }

        commentCount(someId).then(function(count){
        // do stuff with your html here
        var str ='<div> Count:' + count + '</div>';
        })





        share|improve this answer















        Your function is returning a promise and you have to resolve that promise before you can get at the data



        It is also using a needless $.Deferred since getData() returns a promise



        Needs to be something like:



        function commentCount(id) {

        var url ='....';

        // return the getData promise
        return getData(url).then(function(data) {
        // return the count to next `then()` in chain
        return data.d.results.length
        })

        }

        commentCount(someId).then(function(count){
        // do stuff with your html here
        var str ='<div> Count:' + count + '</div>';
        })






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 23:50

























        answered Nov 23 '18 at 23:44









        charlietflcharlietfl

        139k1389122




        139k1389122
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453873%2fget-deferred-promise-value-jquery%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin