in Education by
There are a lot of samples on running Async/Await functions. But only with one await call. I don't find a right sample that I can run await functions one after one async function a() { console.log('this is function a'); // do something } async function b() { console.log('this is function b'); // do something } async function c() { console.log('this is function c'); // do something } async function main() { await a(); await b(); await c(); } main(); Run code snippetExpand snippet Test with above code (hide the real code), I found it is not guaranteed, function c will be executed last. How can I adjust it? Node version: $ node --version v10.15.3 JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Like the comments on your question say: your example code will always run in the right order. In the comments you mention that if there are hundreds of lines of code, it does not always run that way. Could it be that somewhere in that code you use things like setTimeout()? Since setTimeout runs in a separate execution context, the async/await will not wait for it to finish. I have updated your example to demonstrate this. async function a() { console.log('this is function a - 1'); await getResponse(); console.log('this is function a - 2'); } async function b() { console.log('this is function b - 1'); setTimeout(()=>{ console.log('this is function b - 2') }, 500); } async function c() { console.log('this is function c'); } async function main() { await a(); await b(); await c(); } function getResponse() { return new Promise(function(resolve, reject) { setTimeout(function() { console.log('setTimeout finished'); resolve("Response from API. Executed after 5 secs"); }, 500); }); } main(); Run code snippetExpand snippet

Related questions

0 votes
    My websocket application accepting connections like this: wsServer.on('request', function(request) { console.log( ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    I'm trying to debug my nodejs app using node-inspector. But Google Chrome doesn't show the code. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    app.route('/users') .post(user.post) .get(user.get) .get(user.everyone) .put(user.update) . ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    Quick Summary of my question: Does Gulp 3.6.0 dest() handle glob-base the same way as 4.0.0? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    I am trying to add an if condition; Idea is If I enter the input as print or text with 4 in ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am trying to add an if condition; Idea is If I enter the input as print or text with 4 in ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am trying to add an if condition; Idea is If I enter the input as print or text with 4 in ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am trying to write some stuff in a file using reactJS and nodeJs but it doesnt work. Here is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am trying to write some stuff in a file using reactJS and nodeJs but it doesnt work. Here is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I'm used to v3 node botbuilder sdk so I have a middleware where I look at the dialog stack and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    The napi_create_date function appeared in Node.js starting with 11.11.0 version. https://nodejs.org/api/n- ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I'm trying to include actual date and time to the certain message, however I'm unable to do it, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I have working (stock) script from node var cluster = require('cluster'); var http = require(' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I copied and pasted a for-await-of example from MDN and still get an error telling me await is a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I copied and pasted a for-await-of example from MDN and still get an error telling me await is a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
...