Add resolveAfter

This commit is contained in:
Chalarangelo
2022-01-07 20:32:05 +02:00
parent a7eba42142
commit 96f743b2f5

22
snippets/resolveAfter.md Normal file
View File

@ -0,0 +1,22 @@
---
title: resolveAfter
tags: function,promise,intermediate
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
```