Javascript 如何使用node.js中的承诺或同步调用处理if语句
我正在使用Stripe构建一个签出流,并运行一系列处理支付事件的函数。在许多情况下,必要的客户信息将存储在我的终端上(如Javascript 如何使用node.js中的承诺或同步调用处理if语句,javascript,ajax,node.js,promise,Javascript,Ajax,Node.js,Promise,我正在使用Stripe构建一个签出流,并运行一系列处理支付事件的函数。在许多情况下,必要的客户信息将存储在我的终端上(如customerToken),我可以将这些信息传递给向客户收费的代码,一切正常 但是,我不太确定如何处理这样一个事件:用户没有customerToken,在我从Stripe API返回一些内容之前,必须在创建时不重复一堆代码 我尝试在创建customerToken的函数上设置一个承诺,但是——假设我没有完全误解承诺——如果信息已经存在并且代码永远不会执行,那么这个承诺就不会实现
customerToken
),我可以将这些信息传递给向客户收费的代码,一切正常
但是,我不太确定如何处理这样一个事件:用户没有customerToken
,在我从Stripe API返回一些内容之前,必须在创建时不重复一堆代码
我尝试在创建customerToken的函数上设置一个承诺,但是——假设我没有完全误解承诺——如果信息已经存在并且代码永远不会执行,那么这个承诺就不会实现
我还看到有一些节点模块强制调用等待(如下所示),但我不熟悉节点,不知道是否有一种更真实的节点处理方式
我现在拥有的可以简化为这样:
// 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
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
});
我不知道我是在要求一些根本不存在的东西,还是缺少一个基本的概念,直到现在我还没有找到一个明确的答案。对不起,如果这是显而易见的 您可以使用一个结构,它总是返回一个通过
customerToken履行的承诺,如下所示:
// 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
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
});
其思想是,此函数始终返回一个承诺,即在解析时,其解析值将是客户令牌。在某些情况下,承诺会立即得到解决,而在另一些情况下,需要更长的时间才能解决
因此,如果令牌已经可用,您只需返回一个已解析的承诺,并将该令牌作为承诺值。如果令牌不可用,则返回一个承诺,该承诺将在令牌可用时使用该令牌进行解析。然后,打电话的人就可以使用承诺,而不必知道走了哪条路。啊,完全做到了——当然你可以立即解决:)非常感谢。