Promise Delay
Promise Delay
Create a function that resolves with a value after a delay.
Requirements:
- Implement
delaythat takes a value and milliseconds - Return a Promise that resolves with the value after the delay
- Support chaining with other promises
- Handle zero and negative delays (resolve immediately)
Example:
delay('Hello', 1000).then(value => {
console.log(value); // 'Hello' (after 1 second)
});
// Chaining
Promise.resolve(5)
.then(n => delay(n * 2, 500))
.then(result => console.log(result)); // 10 (after 500ms)
Examples:
Input 1:
{"value":"hello","ms":100}Output 1:
"hello"Input 2:
{"value":42,"ms":0}Output 2:
42Loading editor...
Run your code to see results
Click the Run button above