Skip to content

Commit

Permalink
Merge pull request #250 from asemarian/master
Browse files Browse the repository at this point in the history
book6, ch4: 5 new questions
  • Loading branch information
austintackaberry committed Aug 15, 2020
2 parents 5d41347 + b96ced1 commit 3f1178b
Showing 1 changed file with 124 additions and 0 deletions.
124 changes: 124 additions & 0 deletions src/data/ES6Beyond/ch4.js
Expand Up @@ -20,6 +20,21 @@ const Ch4Questions = [
explanation:
'If there are no errors, then the Promise will get resolved, but if an error occurs typically the Promise will get rejected.',
},
{
question: 'A promise can only be resolved (fulfilled or rejected) once',
questionId: 'Llq2fzjMh81c6BpCh5Dq',
shouldBeRandomized: false,
answers: [
{
text: 'True',
id: 0,
},
{ text: 'False', id: 1 },
],
correctAnswerId: 0,
moreInfoUrl: 'https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/es6%20%26%20beyond/ch4.md#promises',
explanation: "Promises can only be resolved (fulfillment or rejection) once. Any further attempts to fulfill or reject are simply ignored. Thus, once a Promise is resolved, it's an immutable value that cannot be changed."
},
{
question: `This is a valid way to construct a promise:
${'```js'}
Expand Down Expand Up @@ -70,6 +85,32 @@ ${'```'}
explanation:
'`done` is not a valid method for a Promise. The code above would be valid if it were replaced with `.then()`',
},
{
question: 'What do `.then(..)` and `.catch(..)` methods return when invoked on a promise?',
questionId: 'dyrVkcETZ8s7ipq1ieZ3',
shouldBeRandomized: true,
answers: [
{
text: 'another promise',
id: 0
},
{
text: 'an object',
id: 1
},
{
text: '`undefined`',
id: 2
},
{
text: 'a function',
id: 3
}
],
correctAnswerId: 0,
moreInfoUrl: 'https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/es6%20%26%20beyond/ch4.md#making-and-using-promises',
explanation: "Both `then(..)` and `catch(..)` automatically construct and return another promise instance, which is wired to receive the resolution from whatever the return value is from the original promise's fulfillment or rejection handler (whichever is actually called)"
},
{
question: `What are thenables?`,
questionId: 'fw3mKgWDPvDorEt1J7gi',
Expand Down Expand Up @@ -121,6 +162,89 @@ ${'```'}
explanation:
'`Promise.resolve` creates a resolved promise. To access the value `42`, you need to call `.then()`',
},
{
question: `What will be logged to the console when the following is executed?
${'```js'}
const a = 42, b = Promise.resolve(43), c = 44;
Promise.all([a, b, c]).then(val => console.log(val));
${'```'}
`,
questionId: '2eNwGVoqyK8G9fTf8aBX',
shouldBeRandomized: true,
answers: [
{
text: '`42`',
id: 0
},
{
text: '`43`',
id: 1
},
{
text: '`[42, 43, 44]`',
id: 2
}
],
correctAnswerId: 2,
moreInfoUrl: 'https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/es6%20%26%20beyond/ch4.md#promise-api',
explanation: "`Promise.all([ .. ])` accepts an array of one or more values (e.g., immediate values, promises, thenables). It returns a promise back that will be fulfilled if all the values fulfill, or reject immediately once the first of any of them rejects"
},
{
question: `What will be logged to the console when the following is executed?
${'```js'}
const a = 42, b = Promise.resolve(43), c = 44;
Promise.race([a, b, c]).then(val => console.log(val));
${'```'}
`,
questionId: 'CjctOIwGZTztV155yKQC',
shouldBeRandomized: true,
answers: [
{
text: '`42`',
id: 0
},
{
text: '`43`',
id: 1
},
{
text: '`[42, 43, 44]`',
id: 2
}
],
correctAnswerId: 0,
moreInfoUrl: 'https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/es6%20%26%20beyond/ch4.md#promise-api',
explanation: "While `Promise.all([ .. ])` waits for all fulfillments (or the first rejection), `Promise.race([ .. ])` waits only for either the first fulfillment or rejection."
},
{
question: `What will be logged to the console when the following is executed?
${'```js'}
const a = Promise.resolve(43), b = 42, c = Promise.reject("Oops");
Promise.race([a, b, c])
.then(val => console.log(val))
.catch(e => console.log(e));
${'```'}
`,
questionId: 'H9b44hy04P9rCW5TGewO',
shouldBeRandomized: true,
answers: [
{
text: '`42`',
id: 0
},
{
text: '`43`',
id: 1
},
{
text: '`"Oops"`',
id: 2
}
],
correctAnswerId: 1,
moreInfoUrl: 'https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/es6%20%26%20beyond/ch4.md#promise-api',
explanation: "While `Promise.all([ .. ])` waits for all fulfillments (or the first rejection), `Promise.race([ .. ])` waits only for either the first fulfillment or rejection (whichever happens first). In the code snippet above, if we changed the order of the array to `Promise.race([c, b, a])` then we'd get `'Oops'` printed instead."
}
];

export default Ch4Questions;

0 comments on commit 3f1178b

Please sign in to comment.