Async functions not working?

#1

Sample code:

async function run() {
  console.log('running');
  await wait(1000);
  console.log('done'); // !!?
}

async function wait(ms) {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      console.log('before resolve');
      resolve();
      console.log('after resolve');
    }, ms);
  });
}

run();

No error is shown and the code after await is never executed.
Asynchronous functions were introduced in the Node.js 8.

Ps: I love this app :smile:

0 Likes

#2

Hi Harr :wave:, thank you for your feedback!

We’ll investigate this, we had a similar problem in the past and I’m sure the problem you report must be linked. Thanks for flagging!

0 Likes

#3

Looks like the problem is with the async/await keywords and not with the Promise.
This code is a workaround:

function wait(ms) {
    return new Promise(function (resolve, reject) {
        setTimeout(function () {
            console.log('before resolve');
            resolve();
            console.log('after resolve');
        }, ms);
    });
}

wait(1000).then(() => { // first async call
    console.log('done 1');
    return wait(1000);  // second async call
}).then(() => {
    console.log('done 2');
});

The stange part is if I modify the function to async function then the code jams.

0 Likes

#4

Hi Harr :wave:

Thanks for investigating. Upgrading the Node.js version seem to have fixed the issue although it’s going to take a while for us to release a Node.js version update as it has created other issues we need to patch. We’ll keep you updated and once again thanks for reporting this issue :slightly_smiling_face:.

0 Likes

#5

Hi again Harr :wave:

1.7.0 fixed the issue. Thanks for your bug report :slight_smile:

0 Likes

#6

Thanks! You are awesome :smile:

1 Like