What does Promisify do?











up vote
1
down vote

favorite












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
    2 days ago















up vote
1
down vote

favorite












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
    2 days ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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 2 days ago









KuchBhi

511118




511118












  • "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
    2 days ago


















  • "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
    2 days ago
















"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
2 days ago




"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
2 days ago












1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










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',
    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
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    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



























      up vote
      3
      down vote



      accepted










      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

























        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted






        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 2 days ago

























        answered 2 days ago









        Pedro Silva

        35910




        35910






























             

            draft saved


            draft discarded



















































             


            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




















































































            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python