0

1.js file

var a = function(){
    var dfd = $q.defer();
    http1().then( function( response1 ) {
        http2(response1).then(function( response2 ) {
        dfd.resolve(response2);
    ));
    return dfd.promise;
}

2.js file

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.


    }
)

But if we have a facility of global temporary variable then it will require a minor change.

This variable should be temporary because it should not interfere the next request.

The scope of this variable should be for onerequest only.

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
    }
)

Is there any nice way to do this?

user7131571
  • 239
  • 2
  • 3
  • 12

1 Answers1

0

I wonder why you use jQuery at node.js (while you can use Promise for promises and you do not need it at all for other).

But any way, you can use singleton like this (I mean you can save response1 to x at 1.js and get it from x at 2.js)

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 = {}
Artem Dudkin
  • 588
  • 3
  • 11
  • This looks a good solution. But just for info: Is there any way to have a global temporary variable for a request which I was looking for. – user7131571 Dec 19 '16 at 13:06
  • window.GlobalTempVariable = {response1:response1} ? – Artem Dudkin Dec 19 '16 at 14:23
  • We can't return multiple values from a promise http://stackoverflow.com/questions/28703625/how-do-you-properly-return-multiple-values-from-a-promise – user7131571 Dec 19 '16 at 14:35
  • We can't even define window.GlobalTempVariable in nodejs.:( – user7131571 Dec 19 '16 at 14:47
  • @user7131571 you broke my world – Artem Dudkin Dec 19 '16 at 15:39
  • Yeah that is one of the solution that I figured out. But the drawback with this method is that when we make another request to node service the old data will be there in x.js file. One thing is that we can do is clear the data at end. – user7131571 Dec 19 '16 at 16:24
  • Yeah that is one of the solution that I figured out. But the drawback with this method is that when we make another request to node service the old data will be there in x.js file. One thing is that we can do is clear the data at end. But that is not a good way to handle that when you work on large project. When many people are working on project and they missed out to clear the data. I am lookin for doing this in clean way. Is there any better way of doing this? – user7131571 Dec 19 '16 at 16:26
  • Oh. Looks like then you want 'data caching' at your project. This is completely another scope of work :) – Artem Dudkin Dec 19 '16 at 17:07
  • There are plenty of libs, all with different caching strategies, for instance, lru-cache, node-cache, flat-cache – Artem Dudkin Dec 19 '16 at 17:11