I apologize if the question is stupid, I just started to deal with async/await.
There is a function whose callback needs to be executed asynchronously, but it calls another asynchronous function many times -
....then(async(data)=>{//Need asynchronous execute this callback
	try {
		let size=data.size;
		data.forEach((message)=>{
			if(! message.pinned) {
				await message.delete().catch(writeErr);//message.delete - returns Promise
			}
		});
		Send(size +"messages deleted");//This call should work only after all the Promise from message.delete is completed
	} catch(err) {
		Send("Sorry, an error occurred");
		return writeErr(err);
	}
})

This code throws an error -
await message.delete().Catch(writeErr);
      ^^^^^^^
SyntaxError: Unexpected identifier
    at createScript(vm.js: 53: 10)
    at Object.runInThisContext(vm.js: 95: 10)
    at Module._compile(module.js: 543: 28)
    at Object.Module._extensions..js(module.js: 580: 10)
    at Module.load(module.js: 488: 32)
    at tryModuleLoad(module.js: 447: 12)
    at Function.Module._load(module.js: 439: 3)
    at Module.runMain(module.js: 605: 10)
    at run(bootstrap_node.js: 427: 7)
    at startup(bootstrap_node.js: 151: 9)

How can I realize my plans and what am I doing wrong?

1 Answers 1

The error is due to the fact that the function you pass to forEach is not asynchronous.Those.You need to:
data.forEach(async(message)=>{
      if(! message.pinned) {
          await message.delete().catch(writeErr);//message.delete - returns Promise
      }
});
  • Those. if I make it asynchronous, will everything work as planned?
    Send (size +"messages deleted");
    Will it work after the messages are deleted?
    – Unforgettable NASCAR Mar 16 '18 at 18:31
  • Yes – Unholy Plumber Mar 16 '18 at 18:44
  • Thank) – Unforgettable NASCAR Mar 16 '18 at 18:47
  • Checked - does not work. The Send function is called immediately, and only then messages are deleted. – Unforgettable NASCAR Mar 16 '18 at 21:34
  • tried through a normal loop?
    for (let i=0;i <data.length;i ++) {if (! data [i] .pinned) {await data [ i] .delete(). catch (writeErr);}}
    – Unholy Plumber Mar 19 '18 at 12:02