【发布时间】:2017-05-01 01:49:59
【问题描述】:
我是 Web 开发新手,最近我开始面临承诺,所以在下面的代码中,结果会在几毫秒后得到解决,但如果我刷新浏览器并立即单击按钮,我会得到一个“未定义”,那么它应该是这样吗?还是承诺通常必须等待并且不给出任何结果(甚至未定义),除非它已解决或失败或我的实现错误?
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-app',
template: `<button (click)="showMyId()">Next</button>
<h1>S {{myId}} </h1>`,
})
export class AppComponent implements OnInit {
thepeer: any;
myId: any;
ngOnInit(){
this.thepeer = new Peer({key: '1h907r5xnvims4i'});
this.showMyId();
}
showMyId(){
this.getMyId().then((id)=>{
this.myId = id;
console.log(this.myId);
})
}
getMyId(){
return new Promise((resolve, reject)=>{
resolve(this.thepeer.id);
})
}
}
非常感谢您的帮助。
【问题讨论】:
标签: javascript angular peerjs