What is the difference between the following two blocks of code?
<p>I've been working on <code>Node.js</code> and encountered some problem with the following code, the problem is solved, but I want to know why:
(<code>_</code> is <code>lodash</code>, <code>request</code> is the <code>request</code> <code>^2.60.0</code> package in npm)</p>

<pre><code> var send = (options, type) =&gt;
     (req, res, next) =&gt; {
         var defaultOptions = {
             baseUrl: config.urlprefix,
             method: 'GET',
             encoding: 'utf8',
             header: {
                 'Content-Type': 'application/json'
             }
         }
         options = options || {};

         if (_.isFunction(options)) {
             options = _.assign(defaultOptions, options(req));
         }
         else if (_.isObject(options)) {
             options = _.assign(defaultOptions, options);
         }

         request(options, parseResToJSONAndSend(res, type));
     };
</code></pre>

<p>and this does not work well: The second(and more) time I call <code>send</code>, the <code>options</code> parameter in
<code>request</code> is just the same as the first time I call <code>send</code>, even if I changed the params pass to <code>send</code>, and it will never change after that.</p>

<p>So I change the code to the following:</p>

<pre><code> var send = (options, type) =&gt;
     (req, res, next) =&gt; {
         var defaultOptions = {
             baseUrl: config.urlprefix,
             method: 'GET',
             encoding: 'utf8',
             header: {
                 'Content-Type': 'application/json'
             }
         }
         var opt = options || {};

         if (_.isFunction(options)) {
             opt = _.assign(defaultOptions, opt(req));
         }
         else if (_.isObject(options)) {
             opt = _.assign(defaultOptions, opt);
         }

         request(opt, parseResToJSONAndSend(res, type));
     };
</code></pre>

<p>and this time it works well. But I don't know where the problem is.
Can someone help me with this? Thanks!</p>

<hr>

<p>to make it clear:
the def of <code>stock</code> controller:</p>

<pre><code>var apis = {
    stocks: (req) =&gt; ({
        url: SOME_URL
    }),
    ...
};

var mod = {};
Object.keys(apis).map((key) =&gt; {
    mod[key] = request(apis[key]);
});
module.exports = mod;
</code></pre>

<p>the use of <code>stock</code> controller:</p>

<pre><code>router.get('/some-path',stock.stocks);
</code></pre>

<p>def of <code>parseResToJSONAndSend</code>:</p>

<pre><code>var parseResToJSONAndSend = (res, type) =&gt;
    (err, resp, data) =&gt; {
        if (err) {
            console.log("Response err", err);
        } else if (resp.statusCode &lt; 200 || resp.statusCode &gt; 299) {
            console.log("Status code not in range 200..299:", resp.statusCode, resp.request.uri.href);
        } else {
            console.log(resp.request.uri.href, resp.statusCode);
            try {
                if (type == 'json') {
                    res.json(JSON.parse(data));
                }
                else {
                    res.send(data);
                }
            } catch (e) {
                console.log("Err in JSON parse", e);
            }
        }
    };
</code></pre>