freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-an.../es6/create-a-javascript-promise.md

1.3 KiB

id title challengeType forumTopicId dashedName
5cdafbb0291309899753167f Create a JavaScript Promise 1 301197 create-a-javascript-promise

--description--

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:

const myPromise = new Promise((resolve, reject) => {

});

--instructions--

Create a new promise called makeServerRequest. Pass in a function with resolve and reject parameters to the constructor.

--hints--

You should assign a promise to a declared variable named makeServerRequest.

assert(makeServerRequest instanceof Promise);

Your promise should receive a function with resolve and reject as parameters.

assert(
  code.match(
    /Promise\(\s*(function\s*\(\s*resolve\s*,\s*reject\s*\)\s*{|\(\s*resolve\s*,\s*reject\s*\)\s*=>\s*{)[^}]*}/g
  )
);

--seed--

--seed-contents--


--solutions--

const makeServerRequest = new Promise((resolve, reject) => {

});