How do you chain promises but change type from one then to another?
<p>I am relatively new to nodejs/typescript/promises so I am not sure of the correct way to chain promises.</p>

<p>I have a a helper class that calls a REST api to fetch the geolocation based on ip. I am not interested in the entire response, only the city field. How do I correctly return a promise that when resolved gets the city field only?</p>

<pre><code>var rest = require("axios");
const ENDPOINT = "http://freegeoip.net/json/";
@Service()
export class GeoIp {
    city(ip: string): Promise&lt;any&gt; {
        let promise: Promise&lt;any&gt;;
        let p = rest.get(ENDPOINT + ip);
        p.then((response) =&gt; {
            promise = Promise.resolve(() =&gt; {return response.data["city"]});
        }, (error) =&gt; {
            promise = Promise.reject(() =&gt; { return error});
        });
        return Promise.resolve(p).then((data)=&gt;promise);
    }
}
</code></pre>

<p>Here is my test code which fails because the received data object is the original REST response object</p>

<pre><code>import chai = require('chai');
import {GeoIp} from "../../server/services/GeoIp";
var assert = chai.assert;

describe("GeoIp service", () =&gt; {
    let geoIp: GeoIp;
    beforeEach("Initialize service", () =&gt; {
        geoIp = new GeoIp();
    });

    var IP_VALID = "137.118.222.187";
    it(`Check geolocation of ${IP_VALID}`, (done) =&gt; {
        let promise = geoIp.city(IP_VALID);
        promise.then((data) =&gt; {
            console.log(data);
            assert.equal(data, "Traphill");
            done();
        });
    });
});
</code></pre>