The Spring Festival of the year of the Rat has only three words: ‘house ~ house ~ house ~’

First of all, let’s talk about the topic (demand comes, face the wind ~)

/** * Two or more implementation methods meet the following requirements: execute Prints the corresponding ids in sequence. * PS: Try to change only the start function body. ** The output is as follows: * id 0 * ID 1 * ID 2 * ID 3 * ID 4 */

function start(id) {
  execute(id).catch(console.error);
}

// Test code (do not change):

for (let i = 0; i < 5; i++) {
  start(i);
}

function sleep() {
  const duration = Math.floor(Math.random() * 500);
  return new Promise(resolve= > setTimeout(resolve, duration));
}

function execute(id) {
  return sleep().then((a)= > {
    console.log("id", id);
  });
}

Copy the code

See this topic we can think about 30s, think about how to solve this problem, interested in a direct copy of the topic to a handy editor to try to write a write, for the program ape thinking process is still very important


Of course, it’s just my solution. If you have a better solution, please kindly comment in the comment section. You can also scan the code and add the TWO-DIMENSIONAL code at the end of the article to communicate in the wechat group. Are you ready? Baby ~

The following answers only include changes to the body of the start function. After all, we only require changes to the body of the start function

The answer to a

This scheme can meet the requirements, but every time the start function is called, the timer will be cleared, so that the execute function will not be executed until all the calls are pushed. The code is not elegant, so it can barely achieve the function

Note that in this example, we can replace this.x with start.x in the start function. By doing so, we can avoid the exception caused by this being undefined in strict mode, and avoid polluting global variables in non-strict mode

function start(id) {
  if (!this.processList) this.processList = [];
  this.processList.push({ id });
  clearTimeout(this.t);
  this.t = setTimeout((a)= >{(async() = > {let target = this.processList.shift();
      while (target) {
        await execute(target.id);
        target = this.processList.shift();
      }
    })();
  }, 0);
}
Copy the code

As a FEer with pursuit, how can he only stay in plan 1, thinking about ~ ~ ~

The answer to the second

After hard thinking, I finally wrote the most elegant implementation scheme, which does not need the help of timer, the idea is the chain call of Promise, namely then function

function start(id) { start.promises = ! start.promises ? execute(id) : start.promises.then((a)= > execute(id));
}
Copy the code

Here, solve the problem come to an end, the whole problem solving process is still painful, but solve the problem is still quite a sense of accomplishment ~

Students who are interested in front-end technology exchange can scan the public number [Jianwen Number] below the wechat code or join the wechat group of front-end developers, which is convenient for everyone to exchange front-end technology

This article is the original article, only for personal archives, please do not reprint at will. If there is a need for reprint, please be sure to bring the original link when reprint! Code word is not easy, thank you for your cooperation!