如何使用node.js中的promises或同步调用来处理if语句

时间:2022-10-20 18:29:46

I'm building a checkout flow using Stripe and running a series of functions for handling a payment event. In many cases the necessary customer information will be stored on my end (as customerToken) and I'm able to just pass that information to the code that charges the customer and everything works well.

我正在使用Stripe构建一个结帐流程,并运行一系列功能来处理付款事件。在许多情况下,必要的客户信息将存储在我的终端(作为customerToken),我能够将这些信息传递给向客户收费的代码,一切运作良好。

However, I'm not exactly sure how to deal with the event in which the user does not already have customerToken and one must be creating without repeating a bunch of code as stuff fires before I get something returned from the Stripe API.

但是,我不确定如何处理用户尚未拥有customerToken的事件,并且必须在不重复一堆代码的情况下创建,因为在我从Stripe API返回之前会触发一些代码。

I tried setting up a promise on the function that creates the customerToken but - assuming I'm not totally misunderstanding promises - that promise wouldn't be fulfilled if that information already existed and the code would never execute.

我尝试在创建customerToken的函数上设置一个promise,但是 - 假设我并没有完全误解承诺 - 如果该信息已经存在并且代码永远不会执行,则承诺将无法实现。

I've also seen there are node modules that force calls to wait (like this https://github.com/yortus/asyncawait) but I'm new to Node and wondering if there is a more true-to-node way of handling things.

我也看到有一些节点模块强制调用等待(比如这个https://github.com/yortus/asyncawait)但是我是Node的新手并且想知道是否有更真实的节点方式处理事情。

What I have now can be simplified to look like this:

我现在可以简化为这样:

// If the user doesn't already have Stripe issued customer information 
if (customerToken === null){

    function() {

        // Asynchronous call to Stripe to get the data that will be stored as customerToken
        Stripe.customers.create({
            source: stripeToken,
            email: customerEmail,
        }).then(function(customer){
            // store customerToken on my end
            request({
                url:'[anAPI]',
                method: 'POST',
                json: {customerToken: customer}
            });
        });
    }
}

function() {
    // Charge the client based on customerId
    Stripe.charges.create({
        amount: price
        currency: 'usd',
        customer: customerToken.id,
        description: description
    });
}

// a bunch of other code that similarly needs a customerToken to function

I have no idea if I'm asking for something that just can't exist or if I'm missing a basic concept and I haven't been able to find a clear answer searching up until now. Sorry if this is obvious!

我不知道我是否要求一些不存在的东西,或者我是否缺少一个基本概念,而且到目前为止我还没有找到一个明确的答案。对不起,如果这很明显!

1 个解决方案

#1


0  

You can use a structure that always returns a promise fulfilled with the customerToken like this:

您可以使用始终返回customerToken履行的承诺的结构,如下所示:

function getToken() {
    if (customerToken) {
        return Promise.resolve(customerToken);
    } else {
        // Asynchronous call to Stripe to get the data that will be stored as customerToken
        return Stripe.customers.create({
            source: stripeToken,
            email: customerEmail,
        }).then(function(customer){
            // Note: this request() is sent asynchronously and
            // this promise does not wait for it's response
            // You could make it wait by returning a promise here
            // store customerToken on my end
            request({
                url:'[anAPI]',
                method: 'POST',
                json: {customerToken: customer}
            });
            return customer;
        });    
    }
}

getToken().then(function(token) {
    // use the token here
});

The idea is that this function always returns a promise that, when resolved, will have its resolved value be the customer token. In some cases, the promise is immediately resolved, in others it takes longer to resolve.

我们的想法是,此函数始终返回一个承诺,在解决后,其解析后的值将成为客户令牌。在某些情况下,承诺会立即得到解决,而在其他情况下则需要更长时间才能解决。

So, if the token is already available, you just return a resolved promise with that token as the promise value. If the token is not available, you return a promise that will be resolved with the token when it is available. Then, the caller can just use the promise without having to know which path was taken.

因此,如果令牌已经可用,您只需将该令牌作为promise值返回已解决的promise。如果令牌不可用,则返回一个承诺,当令牌可用时,该承诺将使用令牌解析。然后,调用者可以使用promise而无需知道采用了哪条路径。

#1


0  

You can use a structure that always returns a promise fulfilled with the customerToken like this:

您可以使用始终返回customerToken履行的承诺的结构,如下所示:

function getToken() {
    if (customerToken) {
        return Promise.resolve(customerToken);
    } else {
        // Asynchronous call to Stripe to get the data that will be stored as customerToken
        return Stripe.customers.create({
            source: stripeToken,
            email: customerEmail,
        }).then(function(customer){
            // Note: this request() is sent asynchronously and
            // this promise does not wait for it's response
            // You could make it wait by returning a promise here
            // store customerToken on my end
            request({
                url:'[anAPI]',
                method: 'POST',
                json: {customerToken: customer}
            });
            return customer;
        });    
    }
}

getToken().then(function(token) {
    // use the token here
});

The idea is that this function always returns a promise that, when resolved, will have its resolved value be the customer token. In some cases, the promise is immediately resolved, in others it takes longer to resolve.

我们的想法是,此函数始终返回一个承诺,在解决后,其解析后的值将成为客户令牌。在某些情况下,承诺会立即得到解决,而在其他情况下则需要更长时间才能解决。

So, if the token is already available, you just return a resolved promise with that token as the promise value. If the token is not available, you return a promise that will be resolved with the token when it is available. Then, the caller can just use the promise without having to know which path was taken.

因此,如果令牌已经可用,您只需将该令牌作为promise值返回已解决的promise。如果令牌不可用,则返回一个承诺,当令牌可用时,该承诺将使用令牌解析。然后,调用者可以使用promise而无需知道采用了哪条路径。