我正在尝试使用使用承诺的回调函数构建的 API。
为了测试它,我创建了这三个函数:
this.functionResolve = (data) => console.log('resolved: ' + data)
this.functionError = (data) => console.log('error: ' + data)
this.functionSucess = (data) => console.log('success: ' + data)
如果使用正常的回调函数,一切正常,我得到两个日志。(解决和错误/成功取决于 cardBin 通知)
PagSeguroDirectPayment.getBrand({
cardBin: "000000",
complete: this.functionResolve,
success: this.functionSucess,
error: this.functionError
});
要将其转换为承诺,我最终得到了这个:
this.promisifyCallback = function() {
return new Promise((resolve, _success, _error) => {
PagSeguroDirectPayment.getBrand({
cardBin: "000000",
complete: resolve,
success: _success,
error: _error
});
});
}
当我打电话时,this.promisifyCallback().then(this.functionResolve, this.functionSucess, this.functionError)
只会出现解析日志。
如果有人想检查,PagSeguroDirectPayment 对象可在以下位置获得:PagSeguro API