-3

I read it in an article that the code below the handlers in a promise is executed first (asynchronously executes code)

let promise = Promise.resolve();
promise.then(() => alert("promise done!"));
alert("code finished"); // this alert shows first

The above code alerts "code finished" first. To tackle this below is the resolution they have provided

Promise.resolve()
  .then(() => alert("promise done!"))
  .then(() => alert("code finished"));

My question is in real-time issues, we may have 1000 lines of code below these handlers. We can't handle all within then handler. How to synchronously handle this

mplungjan
  • 169,008
  • 28
  • 173
  • 236
AjithKumar
  • 21
  • 6
  • use promise.all() it executes when all the promises are resolved!! – vinayak shahdeo Jan 27 '20 at 18:33
  • 1
    Does this answer your question? [How to wait for a JavaScript Promise to resolve before resuming function?](https://stackoverflow.com/questions/28921127/how-to-wait-for-a-javascript-promise-to-resolve-before-resuming-function) – Álvaro Tihanyi Jan 27 '20 at 18:33
  • 1
    "*We may have 1000 lines of code below these handlers. We can't handle all within then handler.*" - actually yes, you would put everything that needs to run after the promise fulfills inside the handler. Or wrap it in a function and call that from the handler, at least. – Bergi Jan 27 '20 at 18:37
  • @Bergi What ? if I have 100000 lines, we put everything inside a function to make this work synchronously? So, we literally need to write our whole program keeping promise in mind irrespective of its size? – AjithKumar Jan 27 '20 at 18:43
  • Ha, welcome to the world of asynchronous coding @AjithKumar. – Roamer-1888 Jan 27 '20 at 18:44
  • @vinayakshahdeo Can you solve the above problem with promise.all() please? – AjithKumar Jan 27 '20 at 18:46
  • @AjithKumar You cannot make it work synchronously anyway. All you can do is make it execute sequentially. And btw, if you have 100000 lines of code that are not already factored into reusable functions, you're doing something wrong. – Bergi Jan 27 '20 at 18:46
  • @Bergi Ya.Fine.Sequentially. Why do you think I won't reuse my functions. I have 100000 lines of code return in the best way possible. Now, I am adding a promise to alert something before this code. If I have an alert in my 5th line let us say. What would execute first? – AjithKumar Jan 27 '20 at 18:53
  • @Bergi Maybe I am ignorant of this. I just need some clarity. – AjithKumar Jan 27 '20 at 18:54
  • 1
    @Roamer-1888 :) – AjithKumar Jan 27 '20 at 18:55
  • Ajith the promise.all function can only be used with an array. I can do it but it is better to use then for single promise. If you have something like [Promise, Promise, Promise, Promise] then promise.all is really helpful – vinayak shahdeo Jan 27 '20 at 19:03
  • 2
    @AjithKumar None of the 100000 lines of function declarations need to go inside the promise handler. Only the code that actually calls them. – Bergi Jan 27 '20 at 19:09

1 Answers1

0

Read about async/await. It is a way of writing asynchronous code in a synchronous way.

Abhishek Ranjan
  • 498
  • 3
  • 17