当这个 promise 运行时,浏览器就会超时(没有发送任何数据。 ERR_EMPTY_RESPONSE)。终端中没有任何错误。第一个 console.log 被打印,但没有其他内容。
var createStripeCustomer = function(customer, source) {
console.log('create stripe customer');
return new Promise(function(res, rej) {
stripe.customers.create({
email: customer.email,
source: source,
account_balance: 500,
metadata: {
first_name: customer.first_name,
last_name: customer.last_name,
phone: customer.phone,
address: `${customer.address} ${customer.appt}`,
city: customer.city,
state: customer.state,
zipcode: customer.zip,
referral: customer.referral
},
function(err, newCustomer) {
if (err) {
console.log(err);
rej(err);
}
else {
console.log('created customer');
res(newCustomer);
}
}
});
});
};
最佳答案
您在 stripe.customers.create
方法第一个参数中错过了 }
:
var createStripeCustomer = function(customer, source) {
console.log('create stripe customer');
return new Promise(function(res, rej) {
stripe.customers.create({
email: customer.email,
source: source,
account_balance: 500,
metadata: {
first_name: customer.first_name,
last_name: customer.last_name,
phone: customer.phone,
address: `${customer.address} ${customer.appt}`,
city: customer.city,
state: customer.state,
zipcode: customer.zip,
referral: customer.referral
}
},
function(err, newCustomer) {
if (err) {
console.log(err);
rej(err);
}
else {
console.log('created customer');
res(newCustomer);
}
}
});
});
};
然后它应该以这种方式工作:
createStripeCustomer(customer, source).then( res => console.log(res) ).catch(error => console.error(error))
关于javascript - promise 不发送任何数据。没有错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47562881/