typescript 打字稿承诺在 .then 之后丢失值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40660168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:01:09  来源:igfitidea点击:

Typescript promise losing values after .then

angulartypescriptpromise

提问by JSeatter

I have a function within an Angular2 service that is returning Mock data via Promise.resolvewhich when unwrapped with .thengives me an empty promise object. I can see that the calling function is receiving the Promise with the payload in the __zone_symbole__valueproperty before it is passed into .then however inside of.thenI seem to be left with just an empty promise.

我在 Angular2 服务中有一个函数,它返回 Mock 数据,通过Promise.resolve它打开时.then会给我一个空的承诺对象。我可以看到调用函数在__zone_symbole__value传递到.then however inside of.then之前正在接收带有属性中的负载的 PromiseI seem to be left with just an empty promise.

  getTemperatureData(): Promise<any> {

    let data = this.convertJSONToGoogleChartTable(temperatureData_JSON);
    let p = Promise.resolve(data);
    return p;
  }

Using Chrome I see that p above looks like

使用 Chrome 我看到上面的 p 看起来像

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]]"}

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 which is broken into two lines to debug is below.

分为两行进行调试的调用代码如下。

getTemperatureData() {
    var d = this.dataService.getTemperatureData();
    d.then(data => this.line_ChartData = data);
}

When I look at d I see the same as p above

当我查看 d 时,我看到的与上面的 p 相同

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]]"}

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 with the .thenwhere the value of "d" is just an empty promise. The below was taken from the Chrome dev tools console to show what I am seeing.

问题出现.then在“d”的值只是一个空头承诺的地方。以下内容取自 Chrome 开发工具控制台,以显示我所看到的内容。

d.then(data => console.log(data))
ZoneAwarePromise {__zone_symbol__state: null, __zone_symbol__value: Array[0]}

No matter what I do and how many combinations I have tried I cannot get to my data inside of d. (Note that p and d are only temporary to break the code down for now.)

无论我做什么以及尝试了多少组合,我都无法获取 d.d 中的数据。(请注意, p 和 d 只是暂时分解代码。)

My package.json is below:

我的 package.json 如下:

{
  "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"
  }
}

回答by StriplingWarrior

the value of data is just an empty promise

数据的价值只是一个空头承诺

That tells me that convertJSONToGoogleChartTableis returning a promise, and you're not chaining your promise to it.

这告诉我这convertJSONToGoogleChartTable是在返回一个承诺,而你并没有将你的承诺与它联系起来。

Note that if you were using a stronger type than any, the TypeScript compiler would probably have caught this for you.

请注意,如果您使用的是比 更强的类型any,TypeScript 编译器可能会为您捕捉到这一点。

Since you're not doing anything after getting that data, you could just do this:

由于您在获取该数据后没有做任何事情,您可以这样做:

  getTemperatureData(): Promise<any> {
    return this.convertJSONToGoogleChartTable(temperatureData_JSON);
  }

But if you want to do something with that data before returning it, you can do that in a then, chained off of the original promise:

但是如果你想在返回数据之前对它做一些事情,你可以在 a 中做到这一点then,链接到原始承诺:

  getTemperatureData(): Promise<any> {
    return this.convertJSONToGoogleChartTable(temperatureData_JSON)
        .then(data => {
            console.log(data);
            return data;
        });
  }