-1

I have a function that looks like this:

async function sync(req, res, done){
    await createRecords().then(async ()=> {
        await Promise.all(
            [
                quantityReport(),
                listings(),
                productIdentifiers(),
                productChildren()

        ])
    }).then(async ()=>{
        await saveAll()
    } ).then(await createCSV);
}

module.exports = sync

I am calling it like this inside a switch:

// const saveRecords = require('../scripts/saveRecords.js') <- for reference

await saveRecords;

My problem is that the program continues before saveRecords finishes and I cannot figure out why.

All of the functions in Promise.all are asynchronous functions.

If I call sync() directly in saveRecords.js it works fine.

Thanks.

edit

createCSV also works fine in other locations in the program. It's imported to this file like this:

const {createCSV, uploadReports} = require('../scripts/createCSV.js')
//in createCSV.js

module.exports = createCSV;

Jonathan Zier
  • 150
  • 2
  • 14
  • `then(await createCSV);` — You need to pass a **function** to `then` (this is likely your problem but you haven't supplied a [mcve]) – Quentin Jul 06 '21 at 09:32
  • It is, I'll update the question with the import statement. – Jonathan Zier Jul 06 '21 at 09:33
  • 2
    Using `then` with `await` seems like a bit of an anti-pattern. – Andy Jul 06 '21 at 09:33
  • 2
    [Promises are not just callbacks](https://stackoverflow.com/questions/22539815/arent-promises-just-callbacks). Nested `.then()` calls are a code smell. As is `.then(async () =>{/*...*/})`. In general, don't try to mix `await` and `.then`. – VLAZ Jul 06 '21 at 09:34
  • `then( async () => await something_that_returns_a_promise() )` is the nested promise antipattern. It works out to be the same as `then(() => something_that_returns_a_promise())` but with more bloat. – Quentin Jul 06 '21 at 09:34
  • Why is the name of an `async` function `sync`? o.O – Andreas Jul 06 '21 at 09:35
  • ha, this syncs data, I guess it could be named better... – Jonathan Zier Jul 06 '21 at 09:39
  • Did you mean `await saveRecords()`? Because in your example you're not calling the function. – Andy Jul 06 '21 at 09:43
  • 1
    @Andy likely `.then(createCSV);` if `.then()` is to be used. – VLAZ Jul 06 '21 at 09:45
  • adding () to saveRecords seems to be working (program takes around 7 minutes to finish). Why is this? It's exported and imported as above. saveRecords.js exports the sync function. – Jonathan Zier Jul 06 '21 at 09:52
  • 1
    Yes. But `await saveRecords;` doesn't _call_ the function. – Andy Jul 06 '21 at 09:57

2 Answers2

0

I'd refactor your function as such (by the way, sync doesn't sounds like a great name for your function, write something more obvious).

async function sync(req, res, done){
try{
    await createRecords()
    const _res = await Promise.all([
                quantityReport(),
                listings(),
                productIdentifiers(),
                productChildren()
                ])
    if(_res) {
        await saveAll()
        await createCSV()
    }
    return 
   }
    catch(err){
    throw new Error(err)
  }
}

module.exports = sync
DoneDeal0
  • 5,273
  • 13
  • 55
  • 114
0

As I mentioned in the comments using async/await with then from the Promise API (see also fetch) is an odd thing to do. Use one or the other. But the key issue is that you're not calling the sync function await sync().

Here's an quick example of simply using async/await.

function mockCall(n) {
  return new Promise((res, rej) => {
    setTimeout(() => res(n), 1000);
  });
}

async function sync() {

  const first = await mockCall(1);

  const twoToFive = await Promise.all([
    mockCall(2),
    mockCall(3),
    mockCall(4),
    mockCall(5)
  ]);

  const six = await mockCall(6);

  const seven = await mockCall(7);

  console.log([ first, twoToFive, six, seven ]);

}

(async function main() {
  await sync();
  console.log('Let the processing resume!');
})();
Andy
  • 61,948
  • 13
  • 68
  • 95