← Back to list

Interview20: Delay .then execution in Promise

Deep dive into the promise question

✅ open src 🚀 🚀 · 2025-08-22 12:54 · 0 claps · 0.9 min read paywalled
#promises #promises-in-javascript #delay
Open on Medium ↗
Wiki topics: 🌐 · Web Development

Interview20: Delay .then execution in Promise

Deep dive into the promise question

We are trying to solve a promise problem. Promise are the backbone of javascript.

You need to right a function/class that return multiple functions to execute in chain. We also need to add a delay function that should delay the next then function from execution.

Ex:

function TaskRunner() {

  return {
    delay: function() {

    },

    printName: function(name) {

    }
}

TaskRunner()
  .printName("Herry")
  .delay(2000)
  .printName("John")
  .delay(3000)
  .printName("katty")

Output:

Herry
-------- 2sec delay
John
-------- 3sec delay
katty

This the common JS problem to delay the then execution. here we go

function TaskRunner() {
  let queue = Promise.resolve();

  return {
    delay(ms) {
      queue = queue.then(() => new Promise(r => setTimeout(r, ms)));
      return this;
    },
    printName(msg) {
      queue = queue.then(() => console.log(msg));
      return this;
    },
  };
}

TaskRunner()
  .printName("Herry")
  .delay(2000)
  .printName("John")
  .delay(3000)
  .printName("katty")

Thank you for reading until the end. Before you go, Please consider clapping, commenting and following.

Javascript is weird, I like the weird things. Keep Smiling.

**Interview-19: **Common Causes of UI jank

**Interview-20: You are reading, **Delay .then execution

Interview-21: Coming Soon


메타데이터
post_id
7e1fa02bd215
slug
interview20-delay-then-execution-in-promise-7e1fa02bd215
url
https://medium.com/@opensrc0/interview20-delay-then-execution-in-promise-7e1fa02bd215
canonical_url
https://medium.com/@opensrc0/interview20-delay-then-execution-in-promise-7e1fa02bd215
author_url
https://medium.com/@opensrc0
status
ok
fetched_at
2026-07-18 01:08:56