异步等待+ toPromise挂起 [英] async await + toPromise hangs

查看:250
本文介绍了异步等待+ toPromise挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ngx-stripe ,并且createToken返回我已经拥有的Observable尝试将其转换为Promise,以便我可以使用Async/await.但是,诺言似乎没有解决.调试不会显示任何内容,而我的try/Catch块也不会捕获任何错误.

I'm using ngx-stripe , and the createToken returns an Observable which I've tried to convert to a promise so that I can use Async/await. However, it looks as though the promise dosen't resolve. Debugging doesn't reveal anything, and my try/Catch blocks doesn't capture any errors.

我想知道我是否正确使用了toPromise:

I wanted to know whether I'm using the toPromise correctly :

import {
    Elements,
    Element as StripeElement,
    ElementsOptions,
    BankAccountData,
    StripeService
} from 'ngx-stripe';

constructor(
    public stripeService: StripeService,

) {}



async next() {
    let token: any;
    let account: BankAccountData = {
        country: this.country,
        currency: this.currency,
        account_holder_name: this.first_name + " " + this.last_name,
        account_holder_type: this.type,
        account_number: account_number,
        routing_number: routing_number
    };

    console.log("--> Creating Bankaccount Token", account);

    try {
        token = await this.stripeService.createToken("bank_account", account).toPromise();
    } catch (excep) {
        console.log(excep);
    }

    console.log("-->Token Generated : ", token);
}

编辑

调试器-如果有帮助.这是最后一个控制台输出:

EDIT

Debugger - if it helps. This is the last console output:

创建银行帐户令牌{国家:"AU",货币:"aud",account_holder_name:某人名称",account_holder_type:个人",account_number:"000123456",...}

Creating Bankaccount Token {country: "AU", currency: "aud", account_holder_name: "Someone Name", account_holder_type: "individual", account_number: "000123456", …}

*************************编辑********************** ** 我不确定为什么,但是当我创建一个Stackblitz时,代码就起作用了.

*************************EDIT *********************** I'm not sure why, but the code worked when I created a stackblitz.

然后我比较了stackblitz中的库,并更新了我的angular,rxjs,rxjs-compat以匹配stackblitz中的内容,然后再次尝试,得到的结果与以前相同.

I then compared the libraries in stackblitz and updated my angular, rxjs, rxjs-compat to match what was in stackblitz and tried again and I was getting the same result as before.

然后我删除了toPromise()并将其更改为:

I then removed the toPromise() and changed it to :

this.stripeService.createToken("bank_account", account).subscribe(data => {
               console.log(data);
             });

与stackblitz中的内容相比,我不确定是什么限制了我的项目.我不确定如何解决问题所在,我唯一能想到的就是从头开始重建项目.

I'm not sure what is limiting what my project has compared to what's in stackblitz. I'm not sure how to work out what the problem is, and the only thing I can think of is rebuilding the project from scratch.

推荐答案

尝试执行.pipe(take(1)).toPromise().

Try doing .pipe(take(1)).toPromise().

请参阅内部代码中有关rxjs的逻辑. 解决"仅在完成时被调用(请参见下面的代码).因此,根据可观察的工作方式,您可能无法获得分辨率.但是,即使可观察到的对象不完整,也会在每次发射值时调用该订阅,这就是您的订阅起作用的原因. "take(1)"将导致完成,因此应该称呼您的诺言.

See the logic on the internal code for rxjs. "Resolve" only gets called on completion (see code below). So, depending on how your observable works you may not get the resolution. The subscribe, however, will get called even if the observable is not complete, on each value emission which is why your subscribe works. "take(1)" will cause a completion, so that should call your promise.

这是该函数的rxjs代码.

Here is the rxjs code for that function.

Observable.prototype.toPromise = function (PromiseCtor) {
    var _this = this;
    if (!PromiseCtor) {
        if (_root.Rx && _root.Rx.config && _root.Rx.config.Promise) {
            PromiseCtor = _root.Rx.config.Promise;
        }
        else if (_root.Promise) {
            PromiseCtor = _root.Promise;
        }
    }
    if (!PromiseCtor) {
        throw new Error('no Promise impl found');
    }
    return new PromiseCtor(function (resolve, reject) {
        var value;
        _this.subscribe(function (x) { return value = x; }, function (err) { return reject(err); }, function () { return resolve(value); });
    });
};

这篇关于异步等待+ toPromise挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆