0

I'm looking at this article about async/await. It contains the following:

Sometimes, you only want to trigger an asynchronous computation and are not interested in when it is finished. The following code is an example:

async function asyncFunc() {
    const writer = openFile('someFile.txt');
    writer.write('hello'); // don’t wait
    writer.write('world'); // don’t wait
    await writer.close(); // wait for file to close
}

I'm interested in how the function proceeds if there is no await for asynchronous functions calls insdide in this particular case. So can somebody please rewrite the above example using promises so I can better understand what's going on?

EDIT

As I understand, it can be rewritten like this:

async function asyncFunc() {
    const writer = openFile('someFile.txt');
    writer.write('hello'); // don’t wait
    writer.write('world'); // don’t wait

    return writer.close().then(()=>{

    });

}

Thanks

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488

1 Answers1

2
async function asyncFunc() {
    const writer = openFile('someFile.txt');
    writer.write('hello'); // don’t wait
    writer.write('world'); // don’t wait

    return writer.close();
}

ES2015:

function asyncFunc() {
    const writer = openFile('someFile.txt');
    writer.write('hello'); // don’t wait
    writer.write('world'); // don’t wait

    return writer.close();
}

assumption, is that writer.close(); always returns a promise, and the resolution value is not used by the caller.

Walle Cyril
  • 3,087
  • 4
  • 23
  • 55