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?
javascript node.js redis promise
add a comment |
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?
javascript node.js redis promise
"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
add a comment |
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?
javascript node.js redis promise
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
javascript node.js redis promise
asked Nov 10 at 13:00
KuchBhi
756320
756320
"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
add a comment |
"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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 10 at 13:42
answered Nov 10 at 13:29
Pedro Silva
43811
43811
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
"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