【发布时间】:2018-03-05 21:54:29
【问题描述】:
我在existing open source library 中有以下承诺代码:
简化代码如下:
class SignIn extends React.Component {
constructor(props) {
super(props);
// What is the underlying type of this.resolver ?
this.resolver = Promise.resolve();
}
handleMFACancel() {
// Is resolver a function ?
this.resolver(null);
}
handleMFASuccess(session) {
// Is resolver a function ?
this.resolver(session);
}
}
我不明白我们如何将this.resolver 用作函数。
它不应该是一个已解决的承诺,或者可能只是一个承诺。
【问题讨论】:
-
The Promise.resolve(value) method returns a Promise objectdeveloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/… -
this.resolver()返回一个 Promise 对象。如果要再次调用方法/函数,请分配this.resolver = Promise.resolve(不带括号) -
好吧,如果您只是尝试代码,您会发现它会引发类型错误,您不能将 Promise 用作函数。所以是的,代码只是坏了。
-
请注意,他们overwrite
this.resolverin line 59 带有大概是回调函数的东西。当然,所有这些,尤其是第 58 行中的 passing anasync functionas anew Promiseexecutor,只是可怕的可怕代码。 -
@Bergi 我会这样做的。谢谢。
标签: javascript react-native promise