Test Your JavaScript Skills : ES9 (JavaScript 2018) - 44

Photo by RetroSupply on Unsplash

Test Your JavaScript Skills : ES9 (JavaScript 2018) - 44

Part 44

Table of contents

Introduction

Namaste, In this blog I will discuss 10 code scenarios on Javascript .

  1. Identify the output of the below program

     async function* fx() {
         yield await Promise.resolve(1);
         yield await Promise.resolve(2);
         yield await Promise.resolve(3);
       }
    
       let genobj = fx();
       genobj.next().then(v => console.log(v.value));
    

    a.1

    b.2

    c.3

    d.undefined

    e.error

    Answer:a

    Reason:The return value is 1.

  2. Identify the output of the below program

    
     async function* fx() {
         yield await Promise.resolve(100);  
       }
    
       let genobj = fx();
       genobj.next().then(v => console.log());
       genobj.next().then(v => console.log(v.done));
    

    a.true

    b.false

    c.100

    d.undefined

    e.error

    Answer:a

    Reason:The return value is true.

  3. Identify the output of the below program

     async function* fx() {
         yield await Promise.resolve(10);    
       }
    
       async function forawait() {
           for await (let obj of fx()) {
               console.log(obj);
           }
       }
    
       forawait();
    

    a.10

    b.false

    c.true

    d.undefined

    e.error

    Answer: a

    Reason:The value is 10.

  4. Identify the output of the below program

     async function* fx() {
         yield await Promise.resolve(10);    
       }
    
       async function forawait() {
           for await (let obj of fx()) {
               console.log(obj.done);
           }
       }
    
       forawait();
    

    a.true

    b.false

    c.undefined

    d.error

    e.10

    Answer:c

    Reason:The done property does not exist.

  5. Identify the output of the below program

     async function* fx() {
         yield await Promise.reject(10);    
       }
    
       async function forawait() {
           for await (let obj of fx()) {
               console.log(obj);
           }
       }
    
       forawait();
    

    a.10

    b.undefined

    c.true

    d.false

    e.error

    Answer:e

    Reason:Because of unhandled rejection an error is raised.

  6. Identify the output of the below program

     async function* fx() {
         yield await Promise.reject(10);    
       }
    
       async function forawait() {
         try {
           for await (let obj of fx()) {
               console.log(obj);
           }
         }catch (err) {
             console.log(err);
         }
       }
    
       forawait();
    

    a.error

    b.undefined

    c.10

    d.true

    e.false

    Answer:c

    Reason:The rejected value 10 is captured in the catch block.

  7. Identify the output of the below program

    
     async function* fx() {
         yield await Promise.resolve(1);
         yield await Promise.resolve(2);
         yield await Promise.resolve(3);
       }
    
       let genobj = fx();
       genobj.next().then(v => console.log(v.done));
    

    a.1

    b.2

    c.true

    d.false

    e.error

    Answer:d

    Reason:The return value for done is false.

  8. Identify the output of the below program

    
     async function* fx() {
         yield await Promise.resolve(100);  
       }
    
       let genobj = fx();
       genobj.next().then(v => console.log());
       genobj.next().then(v => console.log(v.value));
    

    a.100

    b.error

    c.undefined

    d.1

    e.false

    Answer:c

    Reason:The return value is undefined.

  9. Identify the output of the below program

     async function* fx() {
         yield await Promise.reject(1);    
       }
    
       let genobj = fx();
       genobj.next().then(v => console.log(v.value));
    

    a.error

    b.undefined

    c.false

    d.true

    e.1

    Answer:a

    Reason:Because of unhandled rejection an error is raised.

  10. Identify the output of the below program

    async function* fx() {
        yield await Promise.resolve(10);    
      }
    
      async function forawait() {
          for await (let obj of fx()) {
              console.log(obj.value);
          }
      }
    
      forawait();
    

    a.10

    b.true

    c.false

    d.undefined

    e.error

    Answer:d

    Reason:The value property does not exist.

Did you find this article valuable?

Support Mahavir DS Rathore by becoming a sponsor. Any amount is appreciated!