Files
30-seconds-of-code/snippets/js/s/resolve-promise-after-amount-of-time.md
Angelos Chalaris 9d032ce05e Rename js snippets
2023-05-19 20:23:47 +03:00

27 lines
685 B
Markdown

---
title: Resolve promise after given amount of time
type: snippet
language: javascript
tags: [function,promise]
author: chalarangelo
cover: filter-coffee-pot
dateModified: 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
```