Javascript promises Quizs

promises 퀴즈 오답 노트

Fufilled, Failed, Pending

What state will this promise be in after 0 seconds?

const examplePromise = () => {
  return new Promise((resolve, reject) => {
    if (true) {
      setTimeout(() => resolve("success"), 3000);
    } else {
      setTimeout(() => resolve("failed"), 5000);
    }
  });
};

=> Pending

True or False: promise1 and promise2 both produce the same output.

const examplePromise1 = new Promise((resolve, reject) => {
  reject("Uh-oh!");
});
const examplePromise2 = new Promise((resolve, reject) => {
  reject("Uh-oh!");
});

const onFulfill = (value) => {
  console.log(value);
};
const onReject = (reason) => {
  console.log(reason);
};

const promise1 = examplePromise1.then(onFulfill, onReject);

const promise2 = examplePromise2.then(onFulfill).catch(onReject);

=> true / .catch(onReject) is syntactic sugar for .then(undefined, onReject).

How many parameters does a Promise constructor take?

const example = new Promise( ? ? ? );

=> 1 / A Promise’s constructor has a single parameter, called the “executor function.” The executor function has two parameters – resolve and reject.

What is the value of the argument that is passed to the onReject()?

let onFulfill = (value) => {
  console.log(value);
};
let onReject = (reason) => {
  console.log(reason);
};

const promise = new Promise((resolve, reject) => {
  if (false) {
    resolve("success value");
  } else {
    reject();
  }
});

promise.then(onFulfill, onReject);

=> undefined

What will be printed to the console after running the code provided?

let link = (state) => {
  return new Promise(function (resolve, reject) {
    if (state) {
      resolve("success");
    } else {
      reject("error");
    }
  });
};

let promiseChain = link(true);

promiseChain
  .then((data) => {
    console.log(data + " 1");
    return link(true);
  })
  .then((data) => {
    console.log(data + " 2");
    return link(true);
  });

=> Success 1 Success 2

What is the fulfilled value of Promise.all()?

=> An array

Resource