javascript - How do you chain promises but change type from one then to another? - Stack Overflow

I am relatively new to nodejstypescriptpromises so I am not sure of the correct way to chain promises

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 = "/";
@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();
        });
    });
});

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/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();
        });
    });
});
Share Improve this question asked Nov 12, 2016 at 4:29 PierrePierre 1,3672 gold badges13 silver badges21 bronze badges 1
  • As indicated in the answers, there's no need to create your own promise. Indeed, doing so is an anti-pattern – cartant Commented Nov 12, 2016 at 4:45
Add a ment  | 

2 Answers 2

Reset to default 4
var rest = require("axios");
const ENDPOINT = "http://freegeoip/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.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745246994a4618454.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信