Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
852 views
in Technique[技术] by (71.8m points)

node.js - Stripe promise pending even though it's async/await

I have a separate file created to facilitate stripe-based functions called stripe.js.

In this file I want to create a function that retrieves session object, so I initialize stripe with my test key and create function that I export as so:

const stripe = require('stripe')('my_stripe_key_here');


export async function obtainSession(sid) {
    return await stripe.checkout.sessions.retrieve(sid);
}

Now in the App.vue I first import the function and try to use it:

import { obtainSession } from "./stripe/stripe";

let session = obtainSession(this.session_id)
             .then(session=> {
               return session
             });
console.log(session)

And all I get is a pending promise in the console (Promise < pending >)

I've tried for a few days and tested different approaches from stackoverflow and elsewhere but I cannot seem to get this promise resolved. I am using a valid and existing session ID with no result.

Can anybody tell me what's the problem? Thanks!

question from:https://stackoverflow.com/questions/65921241/stripe-promise-pending-even-though-its-async-await

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
let session = obtainSession(this.session_id)
         .then(session=> {
           return session
         });

Doing this doesn’t mean this will return after the then- it will return immediately with the Promise returned by obtainSession. The continuation will also run once the promise resolves, but that return is basically meaningless in this flow.

You will have to await this call. Or if you log inside the then you’ll see the result.

You could consider retrieving this and awaiting inside mounted or similar, or have the code set session as a module variable. But in all cases this is going to be something that returns a promise you have to wait for outside of tHat assignment.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...