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
2 days ago
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 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
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
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
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 2 days ago
answered 2 days ago
Pedro Silva
35910
35910
add a comment |
add a comment |
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
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
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
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
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
"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