1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-02-05 07:00:25 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/有时间限制的 Promise 对象 [promise-time-limit].html
2023-04-23 22:41:08 +08:00

74 lines
2.0 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>请你编写一个函数,它接收一个异步函数 <code>fn</code>&nbsp;和一个以毫秒为单位的时间 <code>t</code>。它应根据限时函数返回一个有 <strong>限时</strong> 效果的函数。</p>
<p>限时函数是与原函数相同的函数,除非它需要 <code>t</code> 毫秒以上的时间来完成。如果出现了这种情况,请你返回 <code>"Time Limit Exceeded"</code>&nbsp;拒绝这次函数的调用。注意,它应该返回一个字符串拒绝,而不是一个&nbsp;<code>Error</code>&nbsp;</p>
<p>&nbsp;</p>
<p><b>示例 1</b></p>
<pre>
<b>输入:</b>
fn = async (n) =&gt; {
&nbsp; await new Promise(res =&gt; setTimeout(res, 100));
&nbsp; return n * n;
}
inputs = [5]
t = 50
<b>输出:</b>{"rejected":"Time Limit Exceeded","time":50}
<b>解释:
</b>提供的函数设置在 100ms 后执行完成,但是设置的超时时间为 50ms所以在 t=50ms 时拒绝因为达到了超时时间。
</pre>
<p><b>示例 2</b></p>
<pre>
<b>输入:</b>
fn = async (n) =&gt; {
&nbsp; await new Promise(res =&gt; setTimeout(res, 100));
&nbsp; return n * n;
}
inputs = [5]
t = 150
<b>输出:</b>{"resolved":25,"time":100}
<b>解释:</b>
在 t=100ms 时执行 5*5=25 ,没有达到超时时间。
</pre>
<p><b>示例 3</b></p>
<pre>
<b>输入:</b>
fn = async (a, b) =&gt; {
&nbsp; await new Promise(res =&gt; setTimeout(res, 120));
&nbsp; return a + b;
}
inputs = [5,10]
t = 150
<b>输出:</b>{"resolved":15,"time":120}
<b>解释:
</b>在 t=120ms 时执行 5+10=15没有达到超时时间。
</pre>
<p><b>示例 4</b></p>
<pre>
<b>输入:</b>
fn = async () =&gt; {
&nbsp; throw "Error";
}
inputs = []
t = 1000
<b>输出:</b>{"rejected":"Error","time":0}
<b>解释:</b>
此函数始终丢出 Error</pre>
<p>&nbsp;</p>
<p><b>提示:</b></p>
<ul>
<li><code>0 &lt;= inputs.length &lt;= 10</code></li>
<li><code>0 &lt;= t &lt;= 1000</code></li>
<li><code>fn 返回一个 Promise 对象</code></li>
</ul>