Discrepancy in Try Node.js Training module

Chris Wade-Evans 0 Reputation points
2024-12-21T12:37:25.5166667+00:00

In the Try Node.js training module. In the section Add a top-level asynchronous function there is a discrepancy between the code you are asked to add and the explanation of the code. It seems that the explanation refers to an older method.

Add the following code to the index.js file to create an asynchronous HTTP request:

‘’’JavaScript

Copy

import fetch from 'node-fetch';

console.log(start);

try {

const res = await fetch('https://github.com/MicrosoftDocs/node-essentials');

console.log('statusCode:', res.status);

} catch (error) {

console.log(error: ${error});

}

console.log(end);’’’

The https.get method makes an HTTP request to the Node.js website and returns the response. The get method takes two parameters: the URL to request and a callback function that is called when the response is received. The callback function takes a single parameter, res, which is the response object.

Azure Training
Azure Training
Azure: A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.Training: Instruction to develop new skills.
1,933 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Rafael Canto 76 Reputation points
    2024-12-21T13:06:43.26+00:00

    Hi Chris,

    Actually the example uses the promise-based fetch instead of the original callback version. That's why the function is awaitable. I just tried it here with success.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.