Click here to Skip to main content
14,932,691 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to Create two async functions, where first function will wait for 10 sec and returns sum of any numbers of argument of type number(using spread operator) and second function will wait for the first function to finish and add 10 more to the return value of first function and finally returns the output.
How to resolve this using spread operator and promise
I'm new to javascript...I'm thankful for any help towards this

What I have tried:

var a=0;
setTimeout(function() { Addition(); }, 10000);
async function Addition() 
{
  for (var i=0; i < arguments.length; i++) {
  a += arguments[i];    
}
return a;
}
secondFunction();
async function secondFunction()
{
 await Addition();
 a = a + 10;
 return a;
};
 console.log(Addition(2, 3));

Here in above code I tried using async() and await(), please tell me where can I use spread operator and How to use promise in these functions here?
Posted
Updated 16-Mar-21 23:50pm

1 solution

I showed you how to use a Promise for the setTimeout function yesterday[^].
JavaScript
const later = (delay) => new Promise(resolve => setTimeout(resolve, delay));

MDN has details on the spread operator:
Spread syntax (...) - JavaScript | MDN[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900