async await not working properly

前端 未结 3 2020
南方客
南方客 2021-01-29 05:32

I am new to JavasSript\'s async, await and promise features.

What I am doing is,

async function sendTextMessage(te         


        
3条回答
  •  迷失自我
    2021-01-29 05:58

    Note this answer was written against the original question and not later edits made by the OP.


    An async function is asynchronous. Other functions will not wait for it.

    So you call sendTextMessage which calls callSendAPI and then the rest of the program carries on.

    callSendAPI runs asynchronously. It calls request and waits for the promise returned by request to be resolved. When it has resolved, callSendAPI picks up the return value of request (well, it would if you captured the return value) and then continues with the next line (only there isn't a next line).


    async / await do not make asynchronous code synchronous. They just make it look like it in inside the function declared as async which, itself, becomes asynchronous.


    You could put your three function calls in an async function of their own, make sure each one returns a Promise, and then call each of those three with await.


    See also How do I return the response from an asynchronous call?.

提交回复
热议问题