跳至主要內容

2821. 延迟每个 Promise 对象的解析 🔒


2821. 延迟每个 Promise 对象的解析 🔒

🟠   🔗 力扣open in new window LeetCodeopen in new window

题目

Given an array functions and a number ms, return a new array of functions.

  • functions is an array of functions that return promises.
  • ms represents the delay duration in milliseconds. It determines the amount of time to wait before resolving or rejecting each promise in the new array.

Each function in the new array should return a promise that resolves or rejects after an additional delay of ms milliseconds, preserving the order of the original functions array.

The delayAll function should ensure that each promise from functions is executed with a delay, forming the new array of functions returning delayed promises.

Example 1:

Input:

functions = [
   () => new Promise((resolve) => setTimeout(resolve, 30))
],

ms = 50

Output: [80]

Explanation: The promise from the array would have resolved after 30 ms, but it was delayed by 50 ms, thus 30 ms + 50 ms = 80 ms.

Example 2:

Input:

functions = [
() => new Promise((resolve) => setTimeout(resolve, 50)),
() => new Promise((resolve) => setTimeout(resolve, 80))
],

ms = 70

Output: [120,150]

Explanation: The promises from the array would have resolved after 50 ms and 80 ms, but they were delayed by 70 ms, thus 50 ms + 70 ms = 120 ms and 80 ms + 70 ms = 150 ms.

Example 3:

Input:

functions = [
() => new Promise((resolve, reject) => setTimeout(reject, 20)),
() => new Promise((resolve, reject) => setTimeout(reject, 100))
],

ms = 30

Output:[50,130]

Constraints:

  • functions is an array of functions that return promises
  • 10 <= ms <= 500
  • 1 <= functions.length <= 10

题目大意

给定一个函数数组 functions 和一个数字 ms,返回一个新的函数数组。

  • functions 是一个返回 Promise 对象的函数数组。
  • ms 表示延迟的时间,以毫秒为单位。它决定了在新数组中的每个函数返回的 Promise 在解析之前等待的时间。

新数组中的每个函数应该返回一个 Promise 对象,在延迟了 ms 毫秒后解析,保持原始 functions 数组中的顺序。delayAll 函数应确保从 functions 中的每个 Promise 都被延迟执行,形成返回延迟的 Promise 的函数的新数组。

示例 1:

输入:

functions = [
   () => new Promise((resolve) => setTimeout(resolve, 30))
],

ms = 50

输出:[80]

解释: 数组中的 Promise 在 30 毫秒后解析,但被延迟了 50 毫秒,所以总共延迟了 30 毫秒 + 50 毫秒 = 80 毫秒。

示例 2:

输入:

functions = [
() => new Promise((resolve) => setTimeout(resolve, 50)),
() => new Promise((resolve) => setTimeout(resolve, 80))
],

ms = 70

输出:[120,150]

解释: 数组中的 Promise 在 50 毫秒和 80 毫秒后解析,但它们被延迟了 70 毫秒,所以总共延迟了 50 毫秒 + 70 毫秒 = 120 毫秒 和 80 毫秒 + 70 毫秒 = 150 毫秒。

提示:

  • functions 是一个返回 Promise 对象的函数数组
  • 10 <= ms <= 500
  • 1 <= functions.length <= 10

解题思路

  1. 定义延迟函数:首先定义一个 delay 函数。该函数接受一个 Promise promise 和延迟时间 msdelay 函数返回一个新的 Promise,在延迟 ms 毫秒后才解析传入的 promise 的结果。

  2. 转换函数数组:使用 map 方法遍历 functions 数组,为数组中的每个函数 fn 创建一个新的函数。这个新函数调用 delay(fn(), ms),即执行原始 fn,并将其 Promise 结果延迟 ms 毫秒后再解析。

  3. 返回新数组:经过 map 操作,返回一个新的函数数组,数组中的每个函数都经过延迟处理。每次调用时,返回的 Promise 会在指定的延迟后解析。

复杂度分析

  • 时间复杂度O(n),其中 nfunctions 数组的长度。
  • 空间复杂度O(n),因为返回了一个新的函数数组。

代码

/**
 * @param {Array<Function>} functions
 * @param {number} ms
 * @return {Array<Function>}
 */
var delayAll = function (functions, ms) {
	const delay = (promise, ms) =>
		new Promise((resolve) => {
			setTimeout(() => promise.then(resolve), ms);
		});

	return functions.map((fn) => () => delay(fn(), ms));
};