What does Promisify do?












0














I was following Stephen Grinder tutorial where he started using Promisify.



For that he gave very vague explanation saying that redis needs a callback function and he finds that very untidy + redis does not support promises in NodeJS



And afterwards he did something like this



     const redis = require('redis')
const redisURL = 'redis://127.0.0.1:6379';
const redisClient = redis.createClient(redisURL);
const util = require('util')

client.get = util.promisify(client.get)

const cachedBlog = await client.get(req.user.id)


For some reason I found the explanation to be vague, Can someone please explain this in the most human way? like what does he mean and what we are doing?










share|improve this question






















  • "Promises are a popular solution to some of the drawbacks of the callback-style async APIs dominant in node.js libraries. But it's awkward to write an node.js application using promises when all the libraries you want to use are callback-based. Hence Promisify. It converts callback-style APIs to use promises instead." - Source
    – Andreas
    Nov 10 at 13:04
















0














I was following Stephen Grinder tutorial where he started using Promisify.



For that he gave very vague explanation saying that redis needs a callback function and he finds that very untidy + redis does not support promises in NodeJS



And afterwards he did something like this



     const redis = require('redis')
const redisURL = 'redis://127.0.0.1:6379';
const redisClient = redis.createClient(redisURL);
const util = require('util')

client.get = util.promisify(client.get)

const cachedBlog = await client.get(req.user.id)


For some reason I found the explanation to be vague, Can someone please explain this in the most human way? like what does he mean and what we are doing?










share|improve this question






















  • "Promises are a popular solution to some of the drawbacks of the callback-style async APIs dominant in node.js libraries. But it's awkward to write an node.js application using promises when all the libraries you want to use are callback-based. Hence Promisify. It converts callback-style APIs to use promises instead." - Source
    – Andreas
    Nov 10 at 13:04














0












0








0







I was following Stephen Grinder tutorial where he started using Promisify.



For that he gave very vague explanation saying that redis needs a callback function and he finds that very untidy + redis does not support promises in NodeJS



And afterwards he did something like this



     const redis = require('redis')
const redisURL = 'redis://127.0.0.1:6379';
const redisClient = redis.createClient(redisURL);
const util = require('util')

client.get = util.promisify(client.get)

const cachedBlog = await client.get(req.user.id)


For some reason I found the explanation to be vague, Can someone please explain this in the most human way? like what does he mean and what we are doing?










share|improve this question













I was following Stephen Grinder tutorial where he started using Promisify.



For that he gave very vague explanation saying that redis needs a callback function and he finds that very untidy + redis does not support promises in NodeJS



And afterwards he did something like this



     const redis = require('redis')
const redisURL = 'redis://127.0.0.1:6379';
const redisClient = redis.createClient(redisURL);
const util = require('util')

client.get = util.promisify(client.get)

const cachedBlog = await client.get(req.user.id)


For some reason I found the explanation to be vague, Can someone please explain this in the most human way? like what does he mean and what we are doing?







javascript node.js redis promise






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 13:00









NoobieSatan

1,059525




1,059525












  • "Promises are a popular solution to some of the drawbacks of the callback-style async APIs dominant in node.js libraries. But it's awkward to write an node.js application using promises when all the libraries you want to use are callback-based. Hence Promisify. It converts callback-style APIs to use promises instead." - Source
    – Andreas
    Nov 10 at 13:04


















  • "Promises are a popular solution to some of the drawbacks of the callback-style async APIs dominant in node.js libraries. But it's awkward to write an node.js application using promises when all the libraries you want to use are callback-based. Hence Promisify. It converts callback-style APIs to use promises instead." - Source
    – Andreas
    Nov 10 at 13:04
















"Promises are a popular solution to some of the drawbacks of the callback-style async APIs dominant in node.js libraries. But it's awkward to write an node.js application using promises when all the libraries you want to use are callback-based. Hence Promisify. It converts callback-style APIs to use promises instead." - Source
– Andreas
Nov 10 at 13:04




"Promises are a popular solution to some of the drawbacks of the callback-style async APIs dominant in node.js libraries. But it's awkward to write an node.js application using promises when all the libraries you want to use are callback-based. Hence Promisify. It converts callback-style APIs to use promises instead." - Source
– Andreas
Nov 10 at 13:04












1 Answer
1






active

oldest

votes


















3














Promisify is used when you want to convert a callback function into a promise based function. Nowadays, is used promises because let the developers to write more structured code. With callbacks you have a problem called pyramid of doom (http://callbackhell.com/). Where each function is called inside the other and the code starts to grow horizontally. With promises you can use then to call another function. Let me show you.




Callback Example




 a (function (data1) {
b (function (data2) {
c (function (data3) {
d (function (data4) {
e (function (data5) {
f (function (data6) {
// The Egyptions would be jealous of this pyramid!
})
}
})
})
})
})



Promise example




a(data1)
.then(return b(data2))
.then(return c(data3))
.then(return d(data4))
.then(return e(data5))


If you want I can post a better example but I think that this will help you






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%2f53239200%2fwhat-does-promisify-do%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









    3














    Promisify is used when you want to convert a callback function into a promise based function. Nowadays, is used promises because let the developers to write more structured code. With callbacks you have a problem called pyramid of doom (http://callbackhell.com/). Where each function is called inside the other and the code starts to grow horizontally. With promises you can use then to call another function. Let me show you.




    Callback Example




     a (function (data1) {
    b (function (data2) {
    c (function (data3) {
    d (function (data4) {
    e (function (data5) {
    f (function (data6) {
    // The Egyptions would be jealous of this pyramid!
    })
    }
    })
    })
    })
    })



    Promise example




    a(data1)
    .then(return b(data2))
    .then(return c(data3))
    .then(return d(data4))
    .then(return e(data5))


    If you want I can post a better example but I think that this will help you






    share|improve this answer




























      3














      Promisify is used when you want to convert a callback function into a promise based function. Nowadays, is used promises because let the developers to write more structured code. With callbacks you have a problem called pyramid of doom (http://callbackhell.com/). Where each function is called inside the other and the code starts to grow horizontally. With promises you can use then to call another function. Let me show you.




      Callback Example




       a (function (data1) {
      b (function (data2) {
      c (function (data3) {
      d (function (data4) {
      e (function (data5) {
      f (function (data6) {
      // The Egyptions would be jealous of this pyramid!
      })
      }
      })
      })
      })
      })



      Promise example




      a(data1)
      .then(return b(data2))
      .then(return c(data3))
      .then(return d(data4))
      .then(return e(data5))


      If you want I can post a better example but I think that this will help you






      share|improve this answer


























        3












        3








        3






        Promisify is used when you want to convert a callback function into a promise based function. Nowadays, is used promises because let the developers to write more structured code. With callbacks you have a problem called pyramid of doom (http://callbackhell.com/). Where each function is called inside the other and the code starts to grow horizontally. With promises you can use then to call another function. Let me show you.




        Callback Example




         a (function (data1) {
        b (function (data2) {
        c (function (data3) {
        d (function (data4) {
        e (function (data5) {
        f (function (data6) {
        // The Egyptions would be jealous of this pyramid!
        })
        }
        })
        })
        })
        })



        Promise example




        a(data1)
        .then(return b(data2))
        .then(return c(data3))
        .then(return d(data4))
        .then(return e(data5))


        If you want I can post a better example but I think that this will help you






        share|improve this answer














        Promisify is used when you want to convert a callback function into a promise based function. Nowadays, is used promises because let the developers to write more structured code. With callbacks you have a problem called pyramid of doom (http://callbackhell.com/). Where each function is called inside the other and the code starts to grow horizontally. With promises you can use then to call another function. Let me show you.




        Callback Example




         a (function (data1) {
        b (function (data2) {
        c (function (data3) {
        d (function (data4) {
        e (function (data5) {
        f (function (data6) {
        // The Egyptions would be jealous of this pyramid!
        })
        }
        })
        })
        })
        })



        Promise example




        a(data1)
        .then(return b(data2))
        .then(return c(data3))
        .then(return d(data4))
        .then(return e(data5))


        If you want I can post a better example but I think that this will help you







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 13:42

























        answered Nov 10 at 13:29









        Pedro Silva

        548111




        548111






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53239200%2fwhat-does-promisify-do%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

            Landwehr

            Reims

            Javascript gets undefined on array