admin管理员组文章数量:1335361
In the code below, you see me making an HTTP request. I parse the response and pull out and a "token" (a string) . I want to return that token so that its value is assigned to foo.
foo = request.post(
{
url: 'http://10.211.55.4/api/2.0/auth/signin',
body: reqLogin.toString(),
headers: {'Content-Type': 'text/xml'}
},
function(err, response, body) {
if(err) {
console.log(err);
process.exit(1);
}
else {
parseString(body, function (err, result) {
tokencontainer = (result.tsResponse.credentials[0]);
token = tokencontainer.$.token;
console.log('Logged in, token is ', token);
return token;
});
}
}
);
When I run this code, the object type of foo is Request. Can I somehow cast the whole request to "string" so I don't get the request object assigned to foo? I simply want the "token" value assigned to the variable.
Thanks!
In the code below, you see me making an HTTP request. I parse the response and pull out and a "token" (a string) . I want to return that token so that its value is assigned to foo.
foo = request.post(
{
url: 'http://10.211.55.4/api/2.0/auth/signin',
body: reqLogin.toString(),
headers: {'Content-Type': 'text/xml'}
},
function(err, response, body) {
if(err) {
console.log(err);
process.exit(1);
}
else {
parseString(body, function (err, result) {
tokencontainer = (result.tsResponse.credentials[0]);
token = tokencontainer.$.token;
console.log('Logged in, token is ', token);
return token;
});
}
}
);
When I run this code, the object type of foo is Request. Can I somehow cast the whole request to "string" so I don't get the request object assigned to foo? I simply want the "token" value assigned to the variable.
Thanks!
Share Improve this question edited May 29, 2014 at 18:55 Bergi 666k161 gold badges1k silver badges1.5k bronze badges asked May 29, 2014 at 18:49 Russell ChristopherRussell Christopher 1,7073 gold badges21 silver badges36 bronze badges 3- the X and Y problem,look it up.What's the point of trying to set foo when you know foo can only be set in the future,when the request gets a response? people will answer you but it wont help your case.All subsequent operations in your code will have to be async. – mpm Commented May 29, 2014 at 18:55
- How does request return a promise? – Bergi Commented May 29, 2014 at 18:57
-
1
Learn to use local variable, please. All of
foo
,tokencontainer
, andtoken
are implicitly global. – Bergi Commented May 29, 2014 at 18:58
1 Answer
Reset to default 4If you're using Mikeal's request library, .post() method (or any other) does not return a promise (and even if it did, you wouldn't be able to assign a value this way (rather a promise).
So, your options are:
- move your logic which uses "foo" to a separate function and call it form a callback.
- wrap .post() in a promise using one of helper libraries (like Q).
- find anotehr HTTP library which will support promises.
(note: I wrote the code above but have not ran it, so it might not work as-it-is. But it gives you an idea)
Option 1 could look like:
var logicUsingFoo = function(foo) {
// do stuff with foo here
};
request.post({...}, function (err, response, body) {
// ...
logicUsingFoo(token);
});
// Here, you can't really use foo, because request.post() has not yet
// finished. In Javascript, there's no way to pause program execution.
// You need to either use callbacks, or promises.
Option 2
Something like (using Q library):
var promisePost = function(params) {
var q = Q.defer();
request.post(params, function(err, response, body) {
if (err) {
q.reject(err);
} else {
q.resolve(body);
}
};
return q.promise;
}
Then use it:
promisePost({url: ..., headers: ...}).then(function (body) {
// Do stuff with body, obtain token etc.
}, function (err) {
// Handle error
});
Option 3
You could use, for instance, Q-IO by Kris Kowal, the same guy who created widely used Q library for promises.
本文标签: javascriptReturning a string value from a promiseStack Overflow
版权声明:本文标题:javascript - Returning a string value from a promise - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742381241a2464135.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论