Tag: 保证

为什么在使用promise的时候,这个在类方法中是未定义的?

我有一个JavaScript类,每个方法返回一个Q诺。 我想知道为什么this是未定义在method2和method3 。 有没有更正确的方法来编写这段代码? function MyClass(opts){ this.options = opts; return this.method1() .then(this.method2) .then(this.method3); } MyClass.prototype.method1 = function(){ // …q stuff… console.log(this.options); // logs "opts" object return deferred.promise; }; MyClass.prototype.method2 = function(method1resolve){ // …q stuff… console.log(this); // logs undefined return deferred.promise; }; MyClass.prototype.method3 = function(method2resolve){ // …q stuff… console.log(this); // logs undefined return deferred.promise; }; 我可以通过使用bind来解决这个问题: […]

Angular 2找不到Promise,Map,Set和Iterator

在安装angular 2之后,我的脚本编译器不断收到有关找不到Promise,Map,Set和Iterator的错误。 直到现在我忽略了他们,但现在我需要承诺,所以我的代码将工作。 import {Component} from 'angular2/core'; @Component({ selector: 'greeting-cmp', template: `<div>{{ asyncGreeting | async}}</div>` }) export class GreetingCmp { asyncGreeting: Promise<string> = new Promise(resolve => { // after 1 second, the promise will resolve window.setTimeout(() => resolve('hello'), 1000); }); } Additional information: npm -v is 2.14.12 node -v is v4.3.1 typescript v is 1.6 […]