R
Reactiflux

✅ – BIOLOGY SCIENCE – 11-02 Jul 28

✅ – BIOLOGY SCIENCE – 11-02 Jul 28

RRelativPerspektiv7/28/2022
this is my code
const fs = require('fs');
const x = [1, 2, 3 ...]; // array consisting of numbers
const temp = []; // empty array

x.forEach((item) =>
{
fx(item).then((promiseData) =>
{ // fx is a function returning a promise

fs.writeFileSync('path/to.file', promiseData);

if ( (item / 2).toString().includes('.') ) {
temp.push(item);
}

});
});

fs.writeFileSync('path/to/differnet.file', JSON.stringify(temp, null, 4));
const fs = require('fs');
const x = [1, 2, 3 ...]; // array consisting of numbers
const temp = []; // empty array

x.forEach((item) =>
{
fx(item).then((promiseData) =>
{ // fx is a function returning a promise

fs.writeFileSync('path/to.file', promiseData);

if ( (item / 2).toString().includes('.') ) {
temp.push(item);
}

});
});

fs.writeFileSync('path/to/differnet.file', JSON.stringify(temp, null, 4));
for some reason the content of path/to/different.file is just an empty array i have tried logging the temp array after the forEach loop, it is not empty, but when i try to write the file, it doesnt work properly any fixes ?
SScriptyChris7/28/2022
forEach is unaware of async code inside You should rather use Promise.all + array.map() Something like that
const temp = await Promise.all(
x.map((item) => {
return fx(item)
.then((promiseData) => { // fx is a function returning a promise
fs.writeFileSync('path/to.file', promiseData);

if ( (item / 2).toString().includes('.') ) {
return item;
}
});
})
);
const temp = await Promise.all(
x.map((item) => {
return fx(item)
.then((promiseData) => { // fx is a function returning a promise
fs.writeFileSync('path/to.file', promiseData);

if ( (item / 2).toString().includes('.') ) {
return item;
}
});
})
);
RRelativPerspektiv7/28/2022
Ohok I'll give it a try awesome, it works thank you
UUUnknown User7/28/2022
2 Messages Not Public
Sign In & Join Server To View

Looking for more? Join the community!

R
Reactiflux

✅ – BIOLOGY SCIENCE – 11-02 Jul 28

Join Server
Want results from more Discord servers?
Add your server
Recommended Posts