Kodiak
February 25, 2022, 6:30pm
1
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');
};
Apple
February 25, 2022, 7:25pm
2
You need to encapsulate such code (the response pipe thing only) inside a Promise:
You use await to wait till the promise is resolved
Kodiak
February 25, 2022, 8:29pm
3
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));
});
Apple
February 25, 2022, 8:31pm
4
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…
Kodiak
February 25, 2022, 8:50pm
5
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.
Apple
February 25, 2022, 8:53pm
6
I put it there Unless you have another await somewhere else?
Kodiak
February 25, 2022, 9:04pm
7
Hahhaa Nope I missed that, I thought that was a copy of my code.