如何让 superagent return 成为一个承诺
How to make superagent return a promise
我一直在学习 Node/Javascript,从一开始就使用 promises(我不知道如何不使用 promises 并且常常想知道没有他们)。
所以我有时需要 "promisify" 一些简单的东西,比如用 fs
:
读取文件
var readFile = function(path) {
return new Promise(function(fulfill, reject) {
fs.readFile(path, function(err, data) {
if (err) { reject(err); }
else { fulfill(data); }
});
});
};
而且效果很好。现在我需要对 superagent
执行相同的操作,但它使用的链接样式让我卡住了。
var request = require('superagent');
request.get(...).set(...).set(...).end(callback); // stuck!
我想用 returns 承诺的方法替换 end()
方法(或忽略它并添加新方法)。像这样...
var endQ = function() {
return new Promise(function(fulfill, reject) {
this.end(function(err, res) { // "this" is the problem!
if (err) { reject(err); }
else { fulfill(res); }
});
});
};
// then I could say this:
request.get(...).set(...).set(...).endQ().then(function(res) {
// happiness
}).catch(function(err) {
// sad about the error, but so happy about the promise!
});
This question here has all kinds of advice about adding methods to objects, but it's hard to see what is definitive. I was especially worried by this answer。许多建议都围绕着从对象的 "class" 开始并将函数添加到 .prototype
。像这样....
// this part doesn't make sense
var requestInstance = new Request(); // no such thing in request as far as I know
requestInstance.prototype.endQ = endQ; // would be great, but no
看到我的问题了吗?我想要 JS 等效于 "subclassing" 请求 "class" 并添加一个方法,但由于它是一个模块,我需要将请求 class 视为或多或少不透明。
首先superagent already supports promises:
request.get(...).set(...).set(...).then(response => {
// handle it here
});
请注意,与常规 then
不同,这里的 then
并不是一个承诺 - 它实际上 调用 请求并延迟执行。
其次,你想做的很简单:
Object.getPrototypeOf(request.get(...)).endQ = function() { // get to prototype and define
/* your code here */
};
超级代理本身的作用如下:
exports.then = function then(resolve, reject) {
if (!this._fullfilledPromise) {
var self = this;
this._fullfilledPromise = new Promise(function(innerResolve, innerReject){
self.end(function(err, res){
if (err) innerReject(err); else innerResolve(res);
});
});
}
return this._fullfilledPromise.then(resolve, reject);
}
我一直在学习 Node/Javascript,从一开始就使用 promises(我不知道如何不使用 promises 并且常常想知道没有他们)。
所以我有时需要 "promisify" 一些简单的东西,比如用 fs
:
var readFile = function(path) {
return new Promise(function(fulfill, reject) {
fs.readFile(path, function(err, data) {
if (err) { reject(err); }
else { fulfill(data); }
});
});
};
而且效果很好。现在我需要对 superagent
执行相同的操作,但它使用的链接样式让我卡住了。
var request = require('superagent');
request.get(...).set(...).set(...).end(callback); // stuck!
我想用 returns 承诺的方法替换 end()
方法(或忽略它并添加新方法)。像这样...
var endQ = function() {
return new Promise(function(fulfill, reject) {
this.end(function(err, res) { // "this" is the problem!
if (err) { reject(err); }
else { fulfill(res); }
});
});
};
// then I could say this:
request.get(...).set(...).set(...).endQ().then(function(res) {
// happiness
}).catch(function(err) {
// sad about the error, but so happy about the promise!
});
This question here has all kinds of advice about adding methods to objects, but it's hard to see what is definitive. I was especially worried by this answer。许多建议都围绕着从对象的 "class" 开始并将函数添加到 .prototype
。像这样....
// this part doesn't make sense
var requestInstance = new Request(); // no such thing in request as far as I know
requestInstance.prototype.endQ = endQ; // would be great, but no
看到我的问题了吗?我想要 JS 等效于 "subclassing" 请求 "class" 并添加一个方法,但由于它是一个模块,我需要将请求 class 视为或多或少不透明。
首先superagent already supports promises:
request.get(...).set(...).set(...).then(response => {
// handle it here
});
请注意,与常规 then
不同,这里的 then
并不是一个承诺 - 它实际上 调用 请求并延迟执行。
其次,你想做的很简单:
Object.getPrototypeOf(request.get(...)).endQ = function() { // get to prototype and define
/* your code here */
};
超级代理本身的作用如下:
exports.then = function then(resolve, reject) {
if (!this._fullfilledPromise) {
var self = this;
this._fullfilledPromise = new Promise(function(innerResolve, innerReject){
self.end(function(err, res){
if (err) innerReject(err); else innerResolve(res);
});
});
}
return this._fullfilledPromise.then(resolve, reject);
}