excel4node icon indicating copy to clipboard operation
excel4node copied to clipboard

wb.write() is asynchronous but does not return a promise

Open daweimau opened this issue 5 years ago • 5 comments

Where no handler is provided to wb.write(), the current method uses native fs to write to the buffer:

fs.writeFile(fileName, buffer, function (
    if (err) {
        throw err;
    }
});

But fs works asynchronously. It can be implemented to return a promise: see discussion here.

Currently, wb.write() is non-blocking (good), but it should be returning a void promise so that we can await write completion, and not attempt to operate on the file until it is actually written.

I would submit a PR but I'm not confident enough. In my implementation I just recreated the wb.write() method entirely, but correctly awaiting the fs writeFile result. This works correctly for me:

const buffer = await wb.writeToBuffer();
await fs.writeFile(outPath, buffer, function(err) {
    if (err) {
        throw err;
    }
});

EDIT: On review, I am not sure my implementation above works properly. But the problem is real.

daweimau avatar Sep 16 '19 05:09 daweimau

This is a major issue for me. Need write to be async OR the ability to perform a blocking flush/close.

A the moment when generating a report and exiting process.exit(-1); the workbook is not written.

metawrap-dev avatar Oct 22 '20 14:10 metawrap-dev

@natergj Definitely a major issue here.

SimeonRolev avatar Apr 18 '21 18:04 SimeonRolev

Is there a wokaround? How else would you wait for a file to be written before downloading it?

mellowcello77 avatar May 07 '21 10:05 mellowcello77

Is there a wokaround? How else would you wait for a file to be written before downloading it?

@mellowcello77 , @daweimau provided a workaround in this initial question. He writes synchronously to a buffer await wb.writeToBuffer(); and then uses the default await fs.writeFile which is also synchronous.

SimeonRolev avatar May 07 '21 10:05 SimeonRolev

Is there a wokaround? How else would you wait for a file to be written before downloading it?

@mellowcello77 , @daweimau provided a workaround in this initial question. He writes synchronously to a buffer await wb.writeToBuffer(); and then uses the default await fs.writeFile which is also synchronous.

Thank you for clarifying that for me so quickly, it does actually work, just tested 👍🏻

mellowcello77 avatar May 07 '21 11:05 mellowcello77