2020-08-04 15:13:35 +08:00
---
id: 5cdafbb0291309899753167f
2021-02-06 04:42:36 +00:00
title: Create a JavaScript Promise
2020-08-04 15:13:35 +08:00
challengeType: 1
forumTopicId: 301197
2021-01-13 03:31:00 +01:00
dashedName: create-a-javascript-promise
2020-08-04 15:13:35 +08:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
A promise in JavaScript is exactly what it sounds like - you use it to make a promise to do something, usually asynchronously. When the task completes, you either fulfill your promise or fail to do so. `Promise` is a constructor function, so you need to use the `new` keyword to create one. It takes a function, as its argument, with two parameters - `resolve` and `reject` . These are methods used to determine the outcome of the promise. The syntax looks like this:
2020-08-04 15:13:35 +08:00
```js
const myPromise = new Promise((resolve, reject) => {
});
```
2020-12-16 00:37:30 -07:00
# --instructions--
2020-08-04 15:13:35 +08:00
2021-02-06 04:42:36 +00:00
Create a new promise called `makeServerRequest` . Pass in a function with `resolve` and `reject` parameters to the constructor.
2020-08-04 15:13:35 +08:00
2020-12-16 00:37:30 -07:00
# --hints--
2020-08-04 15:13:35 +08:00
2021-02-06 04:42:36 +00:00
You should assign a promise to a declared variable named `makeServerRequest` .
2020-08-04 15:13:35 +08:00
```js
2020-12-16 00:37:30 -07:00
assert(makeServerRequest instanceof Promise);
2020-08-04 15:13:35 +08:00
```
2021-02-06 04:42:36 +00:00
Your promise should receive a function with `resolve` and `reject` as parameters.
2020-08-04 15:13:35 +08:00
```js
2020-12-16 00:37:30 -07:00
assert(
code.match(
/Promise\(\s*(function\s*\(\s*resolve\s*,\s*reject\s*\)\s*{|\(\s*resolve\s*,\s*reject\s*\)\s*=>\s*{)[^}]*}/g
)
);
2020-08-04 15:13:35 +08:00
```
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
const makeServerRequest = new Promise((resolve, reject) => {
});
```