Search code examples
javascriptnode.jsasynchronouspromisees6-promise

async await not working properly


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

What I am doing is,

async function sendTextMessage(text) {
    console.log("----1----");
    var messageData = {
        message: {
            text: text
        }
    };
   await callSendAPI(messageData);
}

async function sendImageMessage(imageUrl) {
    console.log("----2----");
    var messageData = {
        message: {
            url: imageUrl
        }
    };
  await callSendAPI(messageData);
}

async function sendQuickReply(replies) {
    console.log("----3----");
    var messageData = {
        message: {
            text: text,
            quick_replies: replies
        }
    };
   await callSendAPI(messageData);
}
async function callSendAPI(messageData) {
    await request({
        uri: 'https://graph.facebook.com/v2.6/me/messages',
        qs: {
            access_token: config.FB_PAGE_TOKEN
        },
        method: 'POST',
        json: messageData

    }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            var recipientId = body.recipient_id;
            var messageId = body.message_id;

            if (messageId) {
                console.log("Successfully sent message with id %s to recipient %s",
                    messageId, recipientId);
            } else {
                console.log("Successfully called Send API for recipient %s",
                    recipientId);
            }
        } else {
            console.error("Failed calling Send API", response.statusCode, response.statusMessage, body.error);
        }
    });
}

I am calling this functions in one switch case like,

async function send(){
     await sendTextMessage(text);
     await sendImageMessage(img);
     await sendQuickReply(replies)  
}
send()

But while response showssendImageMessage() last because this function may be not getting ready when I am sending imageUrl to generate a response.


Solution

  • 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?.