Week 9
Quiz 23

Quiz 23: Promise

⚠️
Warning: Executing code will replace the current content on this page with the output produced by the executed code. To return to the quiz simply reload the page using CTRL + R.

Question 1

Shows '1 1'?
JavaScript
const idPromise = x => new Promise(resolve => resolve(x));
 
const writer = x => {
    document.writeln(x);
    return idPromise(x);
};
 
idPromise(1)
    .then(writer)
    .then(it => document.writeln(it));

Question 2

Shows '1 undefined'?
JavaScript
const idPromise = x => new Promise(resolve => resolve(x));
 
const writer = x => {
    document.writeln(x);
    return idPromise(x);
};
const filterEven = n => n % 2 === 0 ? n : undefined;
 
idPromise(1)
    .then(writer)
    .then(filterEven)
    .then(writer);

Question 3

Shows '1 1'?
JavaScript
const idPromise = x => new Promise(resolve => resolve(x));
 
idPromise(1)
    .then(it => {
        document.writeln(it);
        return idPromise(it);
    })
    .then(it => document.writeln(it));

Question 4

Shows only '1'?
JavaScript
const idPromise = x => new Promise(resolve => resolve(x));
 
const writer = x => {
    document.write(x);
    return idPromise(x);
};
const filterEven = n => {
    if (n % 2 === 0) return n;
    else throw Error("not even");
};
 
idPromise(1)
    .then(writer)
    .then(filterEven)
    .then(writer)
    .catch(err => { /* do nothing */});

Question 5

Shows '1 1'?
JavaScript
const idPromise = x => new Promise(resolve => resolve(x));
 
idPromise(1)
    .then(it => it)
    .then(it => document.writeln(it));

Question 6

Shows '1'?
JavaScript
const idPromise = x => new Promise(resolve => resolve(x));
 
document.write(idPromise(1));

Question 7

Shows '1 2 2'?
JavaScript
const idPromise = x => new Promise(resolve => resolve(x));
 
const writer = x => {
    document.writeln(x);
    return idPromise(x);
};
const inc = n => n + 1;
const filterEven = n => n % 2 === 0 ? n : undefined;
 
idPromise(1)
    .then(writer)
    .then(inc)
    .then(writer)
    .then(filterEven)
    .then(writer);

Question 8

Shows '1'?
JavaScript
const idPromise = x => new Promise(resolve => resolve(x));
 
idPromise(1)
    .then(it => document.writeln(it));

Question 9

Shows '1 1'?
JavaScript
const idPromise = x => new Promise(resolve => resolve(x));
 
idPromise(1)
    .then(it => document.writeln(it))
    .then(it => document.writeln(it));

Question 10

Shows an Error in the console?
JavaScript
    const idPromise = x => new Promise(resolve => resolve(x));
 
const writer = x => {
    document.writeln(x);
    return idPromise(x);
};
const filterEven = n => n % 2 === 0 ? n : undefined;
 
idPromise(1)
    .then(writer)
    .then(filterEven)
    .then(writer);

Question 11

Shows '1 2'?
JavaScript
const idPromise = x => new Promise(resolve => resolve(x));
 
const writer = x => {
    document.writeln(x);
    return idPromise(x);
};
 
idPromise(1)
    .then(writer)
    .then(it => it + 1)
    .then(writer);

Question 12

Shows '1 1'?
JavaScript
const idPromise = x => new Promise(resolve => resolve(x));
 
idPromise(1)
    .then(it => {
        document.writeln(it);
        return it;
    })
    .then(it => document.writeln(it));

Question 13

Shows '1'?
JavaScript
const idPromise = x => new Promise(resolve => resolve(x));
 
const writer = x => {
    document.writeln(x);
    return idPromise(x);
};
const filterEven = n => n % 2 === 0 ? n : undefined;
 
idPromise(1)
    .then(writer)
    .then(filterEven)
    .then(writer);

Question 14

Shows more than '1'?
JavaScript
const idPromise = x => new Promise(resolve => resolve(x));
 
const writer = x => {
    document.writeln(x);
    return idPromise(x);
};
const filterEven = n => {
    if (n % 2 === 0) return n;
    else throw Error("not even");
};
 
idPromise(1)
    .then(writer)
    .then(filterEven)
    .then(writer)
    .catch(err => { /* do nothing */});