Files
30-seconds-of-code/snippets/resolveAfter.md
Angelos Chalaris 8a6b73bd0c Update covers
2023-02-16 22:24:28 +02:00

25 lines
645 B
Markdown

---
title: Resolve promise after given amount of time
tags: function,promise
author: chalarangelo
cover: filter-coffee-pot
firstSeen: 2022-01-08T05:00:00-04:00
---
Creates a promise that resolves after a given amount of time to the provided value.
- Use the `Promise` constructor to create a new promise.
- Use `setTimeout()` to call the promise's `resolve` function with the passed `value` after the specified `delay`.
```js
const resolveAfter = (value, delay) =>
new Promise(resolve => {
setTimeout(() => resolve(value, delay));
});
```
```js
resolveAfter('Hello', 1000);
// Returns a promise that resolves to 'Hello' after 1s
```