-1

I need to run various steps in a function but steps have to be run in a specific order. I tried to implement an Async function as follow:

async function test() {
    await setTimeout(function() { console.log('1'); }, 5000);
    await setTimeout(function() { console.log('2'); }, 2000);
    console.log('3');
}
test();

The expected results in the console should be 1, 2, 3 but I get 3, 2, 1. It seems that the await parameter is ignored.

EDIT The setTimeout function is used in the illustrative example above only to simulate a heavy task. In my project, I will not use it. Indeed, I need to connect to a database then reformat the results before moving to the next steps. Even by including the async-await, 2 is logged-in before 1. In other words, empty lists are passed to my graphic because the async-await is not taken into consideration. Here my current code:

async function refreshData() {
    myLabel = [];
    myValues = [];
    myJSON = [];
    const sqlite3 = require('sqlite3').verbose();
    let db = new sqlite3.Database(fullPath + '/annex/data.db');

    await new Promise(resolve => {
        db.each('SELECT firstName, age FROM Info;', function(err, row) {
            console.log('1');
            myLabel.push(row.firstName);
            myValues.push(row.age);
            myJSON.push(JSON.stringify(row));
        });
        resolve(myValues);
        resolve(myLabel);
        resolve(myJSON);        
    });

    console.log('2');
    db.close();
    popChart(myLabel, myValues);
    popTable(); 
}
H. Dave
  • 549
  • 3
  • 9
  • 26

4 Answers4

0

You can write a wait function:

const wait = ms => new Promise(resolve => setTimeout(resolve, ms));

async function test() {
  console.log('1');
  await wait(5000);
  console.log('2');
  await wait(2000);
  console.log('3');
}
test();
Lux
  • 17,835
  • 5
  • 43
  • 73
  • The `setTimeout` was used only to simulate heavy tasks. My goal is to connect to a database, collect the information, reformate the results and finally update a graphic. But all steps have to been done in a specific order. I have edited my question accordingly. – H. Dave Jan 26 '20 at 14:19
  • You've rewritten your question to an entire different question – Lux Jan 26 '20 at 15:40
0

I've used the following

function wait(time) {
    return new Promise((resolve) => setTimeout(resolve, time));
}

and then you just await this function in your method.

JoshuaK98
  • 381
  • 1
  • 2
  • 11
0

using this:

async function test() {
    await new Promise(resolve => {
        setTimeout(function() { console.log('1'); resolve(); }, 5000)
    });
    await new Promise(resolve => {
        setTimeout(function() { console.log('2'); resolve(); }, 2000)
    });
    console.log('3');
}  
test();
mojtaba ramezani
  • 1,461
  • 16
  • 15
0

For using await on a function & waiting till the function executes, it must be running on a promise, so you can create a new function delay which calls setTimeout within itself but runs on promise, so it will return once the console is logged after timeout & the calling function will hold the execution until it gets the promise resolved

Read this for better understanding with more examples

function delay (val,time){
  return new Promise((resolve,reject)=>{
    setTimeout(function() { console.log(val); resolve(true) }, time);
  });
}

async function test() {
    await delay('1',5000);
    await delay('2',2000);
    console.log('3');
}

test();
sarfraaz talat
  • 635
  • 4
  • 10