最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What does Promisify do? - Stack Overflow

programmeradmin4浏览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?

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 asked Nov 10, 2018 at 13:00 AlwaysblueAlwaysblue 11.9k44 gold badges140 silver badges252 bronze badges 1
  • "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 Commented Nov 10, 2018 at 13:04
Add a ment  | 

1 Answer 1

Reset to default 18

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./). 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 Egyptians 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

发布评论

评论列表(0)

  1. 暂无评论