Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you chain promises but change type from one then to another?

I am relatively new to nodejs/typescript/promises so I am not sure of the correct way to chain promises.

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?

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

Here is my test code which fails because the received data object is the original REST response object

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

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

    var IP_VALID = "137.118.222.187";
    it(`Check geolocation of ${IP_VALID}`, (done) => {
        let promise = geoIp.city(IP_VALID);
        promise.then((data) => {
            console.log(data);
            assert.equal(data, "Traphill");
            done();
        });
    });
});
like image 931
Pierre Avatar asked Feb 06 '23 17:02

Pierre


2 Answers

var rest = require("axios");
const ENDPOINT = "http://freegeoip.net/json/";
@Service()
export class GeoIp {
    city(ip: string): Promise<string> {
        return rest
            .get(ENDPOINT + ip)
            .then((response) => response.data.city);
    }
}

The then method always returns another Promise and makes Promises chain-able like that.

Looks like the type signature should be Promise<string> as well.

like image 179
Red Mercury Avatar answered Feb 08 '23 06:02

Red Mercury


var rest = require("axios");
const ENDPOINT = "http://freegeoip.net/json/";
@Service()
export class GeoIp {
    city(ip: string): Promise<any> {
        return rest.get(ENDPOINT + ip).then((res) => res.data.city);
    }
}
like image 25
skav Avatar answered Feb 08 '23 06:02

skav