1

I want do_this_after() to run only once slow_running() has finished. When I click the button the desired result is for 1 to appear then after the SlideUp has finished then 2 should appear.

Unfortunately even my code snippet is erroring(!) but I hope it will help illustrate what I am trying to achieve.

PS I only used slideUp as an example of some code that takes a while to execute, I would like to be able to put any slow running code in slow_running() and use promises (not a callback) to have do_this_after() wait until slow_running() has finished.

I need to support IE.

function slow_running() {
  $("#result").append("1<br />");
  $('#slow_thing').slideUp('slow');
  return true
};

function do_this_after() {
  $("#result").append("2<br />");
};

$(document.body).on('click', "#go", function() {
  slow_running().then(do_this_after());
});
<div class="alert alert-success" id="slow_thing">
  Only once this has gone should 2 appear
</div>
<div id="result"></div>
<hr />
<span class="btn btn-primary" id="go">Promises go!</span>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
happysailingdude
  • 185
  • 1
  • 14
  • Same thing as last time you asked this - you need to convert the `slideUp` to a Promise using the method in the linked canonical – CertainPerformance May 26 '20 at 17:21
  • @CertainPerformance "How do I convert an existing callback API to promises?" does not answer my question. My question is not a duplicate of that. – happysailingdude May 26 '20 at 17:22
  • `slideUp` is a callback API. You want to convert it to Promises. It's the same thing – CertainPerformance May 26 '20 at 17:24
  • @CertainPerformance you didn't read my question - "PS I only used slideUp as an example of some code that takes a while to execute, I would like to be able to put any slow running code in slow_running() and use promises (not a callback) to have do_this_after() wait until slow_running() has finished." - many people struggle with the concept of promises (me included) can you please allow someone to help us understand by allowing them to take a look at answering my question *please* – happysailingdude May 26 '20 at 17:25
  • If the *actual* code you need help with doesn't use a callback API, please post it so there's a [MCVE] to look at. (If the actual code does use a callback API, such as with `slideUp`, then look to the canonical for the solution) – CertainPerformance May 26 '20 at 17:29
  • @certainPerformace I used slideUp as a minimal reproducible example - if I were to put my verbose slow running code in the example that wouldn't help anyone. I did wonder about putting a large loop in instead - if I do that would my question be allowed then? – happysailingdude May 26 '20 at 17:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/214686/discussion-between-happysailingdude-and-certainperformance). – happysailingdude May 26 '20 at 17:33

0 Answers0