angular - Typescript promise losing values after .then -
i have function within angular2 service returning mock data via promise.resolve
when unwrapped .then
gives me empty promise object. can see calling function receiving promise payload in __zone_symbole__value
property before passed .then inside of
.theni seem left empty promise.
gettemperaturedata(): promise<any> { let data = this.convertjsontogooglecharttable(temperaturedata_json); let p = promise.resolve(data); return p; }
using chrome see p above looks like
zoneawarepromise {__zone_symbol__state: true, __zone_symbol__value: "[["date","temperature","lowtemperature"],["05/11/2…",69.02,null],["06/11/2016 23:54:34",69.99,null]]"}
the calling code broken 2 lines debug below.
gettemperaturedata() { var d = this.dataservice.gettemperaturedata(); d.then(data => this.line_chartdata = data); }
when @ d see same p above
zoneawarepromise {__zone_symbol__state: true, __zone_symbol__value: "[["date","temperature","lowtemperature"],["05/11/2…",69.02,null],["06/11/2016 23:54:34",69.99,null]]"}
the problem occurs .then
value of "d" empty promise. below taken chrome dev tools console show seeing.
d.then(data => console.log(data)) zoneawarepromise {__zone_symbol__state: null, __zone_symbol__value: array[0]}
no matter , how many combinations have tried cannot data inside of d. (note p , d temporary break code down now.)
my package.json below:
{ "name": "angular2", "version": "0.0.0", "license": "mit", "angular-cli": {}, "scripts": { "start": "ng serve", "lint": "tslint \"src/**/*.ts\"", "test": "ng test", "pree2e": "webdriver-manager update", "e2e": "protractor" }, "private": true, "dependencies": { "@angular/common": "~2.1.0", "@angular/compiler": "~2.1.0", "@angular/core": "~2.1.0", "@angular/forms": "~2.1.0", "@angular/http": "~2.1.0", "@angular/material": "^2.0.0-alpha.9-3", "@angular/platform-browser": "~2.1.0", "@angular/platform-browser-dynamic": "~2.1.0", "@angular/router": "~3.1.0", "core-js": "^2.4.1", "ng2-bootstrap": "^1.1.16", "node-mysql": "^0.4.2", "rxjs": "5.0.0-beta.12", "ts-helpers": "^1.1.1", "zone.js": "^0.6.23" }, "devdependencies": { "@types/jasmine": "^2.2.30", "@types/node": "^6.0.42", "angular-cli": "1.0.0-beta.19-3", "codelyzer": "1.0.0-beta.1", "jasmine-core": "2.4.1", "jasmine-spec-reporter": "2.5.0", "karma": "1.2.0", "karma-chrome-launcher": "^2.0.0", "karma-cli": "^1.0.1", "karma-jasmine": "^1.0.2", "karma-remap-istanbul": "^0.2.1", "protractor": "4.0.9", "ts-node": "1.2.1", "tslint": "3.13.0", "typescript": "~2.0.3", "webdriver-manager": "10.2.5" } }
the value of data empty promise
that tells me convertjsontogooglecharttable
returning promise, , you're not chaining promise it.
note if using stronger type any
, typescript compiler have caught you.
since you're not doing after getting data, this:
gettemperaturedata(): promise<any> { return this.convertjsontogooglecharttable(temperaturedata_json); }
but if want data before returning it, can in then
, chained off of original promise:
gettemperaturedata(): promise<any> { return this.convertjsontogooglecharttable(temperaturedata_json) .then(data => { console.log(data); return data; }); }
Comments
Post a Comment