Sever Action Output Timing

I have a set of server actions running and created a custom module (with an output) that uploads a file to a new folder…

How do I ensure the file is there before moving to the next server action?

Currently putting a wait time of 10 seconds for example works but would like the code to run as soon as possible rather than an set time later.

response.data.pipe(fs.createWriteStream(destinationImagePath))
    .on('error', (err) => { return err, console.log(err) })
    .once('close', () => { return true, console.log('Success') });

if (response.err == true) {
    return ('Error');
} else {
    return ('Success');
};

You need to encapsulate such code (the response pipe thing only) inside a Promise:


You use await to wait till the promise is resolved

Great got it!

    // response.data.pipe(fs.createWriteStream(destinationImagePath))
//     .on('error', (err) => { return err, console.log(err) })
//     .once('close', () => { return true, console.log('Success') });

// if (response.err == true) {
//     return ('Error');
// } else {
//     return ('Success');
// };

return new Promise((resolve, reject) => {
    response.data.pipe(fs.createWriteStream(destinationImagePath))
        .on('error', reject)
        .once('close', () => resolve(destinationImagePath));
});

Isn’t it missing await?

return await new Promise((resolve, reject) => {
    response.data.pipe(fs.createWriteStream(destinationImagePath))
        .on('error', reject)
        .once('close', () => resolve(destinationImagePath));
});

Because, otherwise, it would return a Promise in pending state (it wouldn’t wait for the Promise to resolve). Unless Wappler does some magic behind the hood and waits for the Promise to resolve…

there is an await before the Promise…

not sure but from my short testing it seems to work. Might have to test a bit more.

I put it there :slightly_smiling_face: Unless you have another await somewhere else?

Hahhaa Nope I missed that, I thought that was a copy of my code.