偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

實現(xiàn)一個方法的鏈式調(diào)用

開發(fā) 前端
這個需求是一個典型的異步鏈式調(diào)用場景,尤其是?sleep?延遲后再繼續(xù)執(zhí)行后續(xù)操作。實現(xiàn)時關(guān)鍵在于:每次調(diào)用都注冊一個異步任務(wù);按順序排隊執(zhí)行(使用?Promise?鏈或任務(wù)隊列);每個方法返回?this?以保持鏈式調(diào)用。

題目描述

請使用 TypeScript 實現(xiàn)一個支持鏈式調(diào)用的 Cat 類,要求如下:

  1. cat.say("Tom"):立即輸出hello Tom到控制臺;
  2. cat.sleep(3000):等待 3000 毫秒后再繼續(xù)執(zhí)行后續(xù)任務(wù);
  3. cat.bye("Bye"):輸出 Bye 到控制臺。

調(diào)用示例:

const cat = new Cat();
cat.say("Tom").sleep(3000).bye("Bye");

期望輸出:

hello Tom
# 等待 3 秒...
Bye

題目解析

這個需求是一個典型的 異步鏈式調(diào)用 場景,尤其是 sleep 延遲后再繼續(xù)執(zhí)行后續(xù)操作。實現(xiàn)時關(guān)鍵在于:

  • 每次調(diào)用都注冊一個異步任務(wù);
  • 按順序排隊執(zhí)行(使用 Promise 鏈或任務(wù)隊列);
  • 每個方法返回 this 以保持鏈式調(diào)用。

代碼實現(xiàn)

class Cat {
  private taskQueue: (() => Promise<void>)[] = [];

  constructor() {
    // 自動開始執(zhí)行任務(wù)隊列
    Promise.resolve().then(() => this.runQueue());
  }

  private async runQueue() {
    for (const task of this.taskQueue) {
      await task();
    }
  }

  say(name: string): this {
    this.taskQueue.push(() => {
      return new Promise<void>((resolve) => {
        console.log("hello", name);
        resolve();
      });
    });
    return this;
  }

  sleep(ms: number): this {
    this.taskQueue.push(() => {
      return new Promise<void>((resolve) => {
        setTimeout(resolve, ms);
      });
    });
    return this;
  }

  bye(msg: string): this {
    this.taskQueue.push(() => {
      return new Promise<void>((resolve) => {
        console.log(msg);
        resolve();
      });
    });
    return this;
  }
}

// 使用方式
const cat = new Cat();
cat.say("Tom").sleep(3000).bye("Bye");

說明:

  • taskQueue 是一個異步任務(wù)列表。
  • 每個方法通過 this.taskQueue.push(() => Promise) 添加一個任務(wù)。
  • 構(gòu)造函數(shù)中立即開始異步執(zhí)行任務(wù)隊列。
  • sleep 實現(xiàn)延遲,后續(xù)任務(wù)自然排隊執(zhí)行。

拓展

如果要進行逆向鏈式調(diào)用,如何實現(xiàn)呢?

cat.bye("Bye").sleep(2000).say("Say");

執(zhí)行順序:

Say
# 等待 2 秒...
Bye

所有方法仍然鏈式調(diào)用,但執(zhí)行順序以調(diào)用順序為準,同時 sleep 是延遲“下一個任務(wù)”的執(zhí)行,而不是“當前任務(wù)”的延遲。

class Cat {
  private queue: { fn: () => void; delay: number }[] = [];
  private nextDelay = 0;

  constructor() {
    Promise.resolve().then(() => this.run());
  }

  private async run() {
    for (const task of this.queue) {
      if (task.delay > 0) {
        await new Promise((res) => setTimeout(res, task.delay));
      }
      task.fn();
    }
  }

  private addTask(fn: () => void): this {
    this.queue.push({ fn, delay: this.nextDelay });
    this.nextDelay = 0; // reset after applying
    return this;
  }

  say(msg: string): this {
    return this.addTask(() => {
      console.log(msg);
    });
  }

  bye(msg: string): this {
    return this.addTask(() => {
      console.log(msg);
    });
  }

  sleep(ms: number): this {
    this.nextDelay = ms; // apply to next task
    return this;
  }
}


責任編輯:武曉燕 來源: 宇宙一馬平川
相關(guān)推薦

2009-07-01 14:37:14

JavaScript異

2014-04-14 15:54:00

print()Web服務(wù)器

2023-12-14 12:56:00

鏈式調(diào)用代碼

2022-05-13 07:42:25

JS編程題LazyMan

2021-09-13 20:38:47

Python鏈式調(diào)用

2021-03-12 21:19:15

Python鏈式調(diào)用

2011-05-17 15:13:59

oracle分頁存儲

2011-07-14 14:36:29

Dbgrid多數(shù)據(jù)庫

2009-07-01 14:31:01

JavaScript異

2017-05-11 13:42:49

JavaScriptJQuery DataDOM

2010-09-06 10:38:25

SQL Server語句

2009-08-14 00:55:21

C#程序編譯

2012-08-07 11:28:13

卸載linux

2022-03-14 10:02:03

散列表鏈表哈希表

2016-11-08 18:53:08

編譯器

2016-09-28 17:34:27

JavaScriptvueWeb

2022-03-24 14:58:02

Java散列表編程語言

2012-09-24 11:11:32

HTML5游戲開發(fā)JavaScript

2009-07-22 17:15:04

C#實現(xiàn)

2018-09-18 10:11:21

前端vue.jsjavascript
點贊
收藏

51CTO技術(shù)棧公眾號