简体   繁体   English

async await + toPromise挂起

[英]async await + toPromise hangs

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. 我正在使用ngx-stripe ,而createToken返回一个Observable,我试图将其转换为promise,以便我可以使用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. 调试没有显示任何内容,我的try / Catch块没有捕获任何错误。

I wanted to know whether I'm using the toPromise correctly : 我想知道我是否正确使用了toPromise:

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: 这是最后一个控制台输出:

Creating Bankaccount Token {country: "AU", currency: "aud", account_holder_name: "Someone Name", account_holder_type: "individual", account_number: "000123456", …} 创建Bankaccount令牌{country:“AU”,货币:“aud”,account_holder_name:“某人姓名”,account_holder_type:“个人”,account_number:“000123456”,...}

在此输入图像描述

*************************EDIT *********************** I'm not sure why, but the code worked when I created a stackblitz. 编辑*************************,我我不确定为什么,但是当我创建一个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. 然后我比较了stackblitz中的库并更新了我的angular,rxjs,rxjs-compat以匹配stackblitz中的内容并再次尝试,我得到了和以前一样的结果。

I then removed the toPromise() and changed it to : 然后我删除了toPromise()并将其更改为:

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

I'm not sure what is limiting what my project has compared to what's in stackblitz. 我不确定什么限制了我的项目与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. 我不知道如何解决问题所在,我唯一能想到的就是从头开始重建项目。

Try doing .pipe(take(1)).toPromise(). 尝试做.pipe(take(1))。toPromise()。

See the logic on the internal code for rxjs. 请参阅rxjs内部代码的逻辑。 "Resolve" only gets called on completion (see code below). “Resolve”仅在完成时被调用(参见下面的代码)。 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. “take(1)”将导致完成,所以应该致电你的承诺。

Here is the rxjs code for that function. 这是该函数的rxjs代码。

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); });
    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与AngularFire2异步/等待toPromise()方法将无法正常工作? - Async / Await with AngularFire2 toPromise() method wont work? 我可以将 Angular toPromise 方法的 http 客户端转换为异步并等待吗? - Can I convert http client of Angular toPromise method into async and await? StackBlitz 中的错误:“意外的严格模式保留字”试图将 async/await 与 subscription.toPromise() 一起使用 - Error in StackBlitz: 'Unexpected strict mode reserved word' trying to use async/await with subscription.toPromise() Angular - async/await Observable toPromise 更新广播新 BehaviorSubject 数据返回 Promise - Angular - async/await Observable toPromise update broadcast new BehaviorSubject data return Promise 打字稿 await toPromise 类型转换错误 - Typescript await toPromise type casting error Jasmine 带有 toPromise 的 Cold Marble - 永无止境的等待 - Jasmine Cold Marble with toPromise - never ending await 使用 toPromise() 和 observable 不能异步工作 - using toPromise() and observable doesn't work async 在 Observable 上使用 await 时,我应该使用什么来代替 toPromise()? - What should I use instead of toPromise() when using await on an Observable? 未定义async / await __generator - async/await __generator is not defined 实现 async/await angular 7 - Implement async/await angular 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM