1.js文件
var a = function(){
var dfd = $q.defer();
http1().then( function( response1 ) {
http2(response1).then(function( response2 ) {
dfd.resolve(response2);
));
return dfd.promise;
}
2.js文件
a().then(function( response ){
// this response is response2
// but what in future I wanted to access response1
// one way is change the response of a() and include response2 in a object
// but this will require a lot of changes at all places where a() is called.
}
)
但是如果我们有一个全局临时变量的设施,那么它将需要一个小的改变。
此变量应该是临时的,因为它不应干扰下一个请求。
此变量的范围应仅适用于onerequest。
var a = function(){
var dfd = $q.defer();
http1().then( function( response1 ) {
GlobalTempVariable.response1 = response1;
http2(response1).then(function( response2 ) {
dfd.resolve(response2);
));
return dfd.promise;
}
2.js
a().then(function( response ){
// I will be able to access response2
// GlobalTempVariable.response1
}
)
有没有好办法呢?
答案 0 :(得分:0)
我想知道为什么你在node.js上使用jQuery(虽然你可以使用Promise作为promises和you do not need it at all for other)。
但是无论如何,你可以像这样使用单身(我的意思是你可以将响应1保存到1.js的x并从2.js处获得x)
1.js file
---------
var x = require('./x');
var a = function(){
var dfd = $q.defer();
http1().then( function( response1 ) {
x.responce1 = response1;
http2(response1).then(function( response2 ) {
dfd.resolve(response2)
})
))
return dfd.promise;
}
2.js file
---------
var x = require('./x');
a().then(function( response2 ){
// You can access response2 as it is
// You can access response1 from x.responce1
})
x.js file
---------
module.exports = {}