-3

I'm studying the promises. What interests me is to understand how to link two functions in a promise.

I'll explain. I want that on completion of one function (if successful) the second function is executed.

For example, I have these two functions very simple; at the end of the sum() function, if successful, the print() function will be executed.

In the case of time-consuming functions, is it worth adding a setTimeout?

Can anyone kindly help me?

function sum(){
  let array = [3, 5, 4, 8];
  let sum = 0;
  array.forEach(item => {
    sum += item;
  });
  console.log(sum);
}

function print(){
  console.log("This function is printed after the sum function if the latter was successful")
 }
 
sum();
print();
Sarah
  • 292
  • 1
  • 10
  • 1
    None of this code us asynchronous. – VLAZ Feb 21 '23 at 18:43
  • 2
    "I want that on completion of one function (if successful) the second function is executed" JS is single threaded and your code is synchronous so you already have this. – Yury Tarabanko Feb 21 '23 at 18:45
  • What you are looking for is called [Promise chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#chaining) (your example notwithstanding). With regard to "In the case of time-consuming functions, is it worth adding a setTimeout?" _absolutely not_, this is what promises and async/await addresses. – msanford Feb 21 '23 at 18:49
  • I was not clear. I know these functions are synchronous. Suppose I turn them into asynchronous, how should I do with a promise? – Sarah Feb 21 '23 at 18:55
  • Then you do `await f1(); await f2();` – Yury Tarabanko Feb 21 '23 at 18:56
  • Promises are a large topic, and _extremely well covered_ in documentation and many blog posts. See the link I posted above, and please return if you have a specific question with a specific [mcve] that does not work, rather than a theoretical question. – msanford Feb 21 '23 at 18:57
  • "Suppose I turn them into asynchronous": putting synchronous code in a promise doesn't magically make it asynchronous. It's just synchronous code in a promise. And there's no point in doing that. – Andy Feb 21 '23 at 19:29

1 Answers1

-4

in case you have two functions that return promises (you might only call them promises) and want to call one after the other completed the simplest thing is to use async/await syntax like

const combinedPromise = async (p1,p2) => {
 await p1();
 await p2();
}

or with pure promises you can chain them like

p1().then(p2());
HannesH
  • 932
  • 2
  • 12
  • 1
    `p1().then(p2());` runs `p1` and `p2` at the same time. Doesn't run `p2` only when `p1` finishes. See [What is the difference between a function call and function reference?](https://stackoverflow.com/q/15886272) and [How does .then(console.log()) and .then(() => console.log()) in a promise chain differ in execution](https://stackoverflow.com/q/50836242) – VLAZ Feb 21 '23 at 21:34
  • @VLAZ you're totally right, will fix as soon as the edit queue lets me – HannesH Feb 22 '23 at 08:12