Categories
JavaScript Answers

How to read the contents of a Node.js stream into a string variable?

Sometimes, we want to read the contents of a Node.js stream into a string variable.

In this article, we’ll look at how to read the contents of a Node.js stream into a string variable.

How to read the contents of a Node.js stream into a string variable?

To read the contents of a Node.js stream into a string variable, we can return a promise that resolves the the combined chunks from the stream.

For instance, we write

const streamToString = (stream) => {
  const chunks = [];
  return new Promise((resolve, reject) => {
    stream.on("data", (chunk) => chunks.push(Buffer.from(chunk)));
    stream.on("error", (err) => reject(err));
    stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
  });
};

to create a promise with the Promise constructor.

We call it with a callback that calls streams.on with 'data' to get the chunk from the stream and then push that to chunks with push.

Then we call on with 'end' with a callback that combines the chunks with

Buffer.concat(chunks).toString("utf8")

And then we call resolve with the buffer converted to a string with toString to use that as the promise resolve value.

Then we write

const result = await streamToString(stream);

in an async function to get the value from the stream.

Conclusion

To read the contents of a Node.js stream into a string variable, we can return a promise that resolves the the combined chunks from the stream.

Categories
JavaScript Answers

How to return result from a promise then() with JavaScript?

Sometimes, we want to return result from a promise then() with JavaScript.

In this article, we’ll look at how to return result from a promise then() with JavaScript.

How to return result from a promise then() with JavaScript?

To return result from a promise then() with JavaScript, we can return a value in an async function.

Then we can use await to get the returned value.

For instance, we write

const request = async (output) => {
  const response = await axios.get(getApiHost() + "/api/some_endpoint");
  return response;
};

const test = async () => {
  const result = await request(output);
  return result + 1;
};

to define the request function that returns the response from the promise returned by axios.get.

And then in test, we use await with request to get the resolve value of the promise returned by request, which is the response.

And then we add 1 to result and return the sum.

Conclusion

To return result from a promise then() with JavaScript, we can return a value in an async function.

Then we can use await to get the returned value.

Categories
JavaScript Answers

How to send a variable number of arguments to a JavaScript function?

Sometimes, we want to send a variable number of arguments to a JavaScript function.

In this article, we’ll look at how to send a variable number of arguments to a JavaScript function.

How to send a variable number of arguments to a JavaScript function?

To send a variable number of arguments to a JavaScript function, we can use the rest syntax.

For instance, we write

const func = (...args) => {
  args.forEach((arg) => console.log(arg));
};

const values = ["a", "b", "c"];
func(...values);
func(1, 2, 3);

to create the func function that takes an unlimited number of arguments with ...args.

All the arguments are in the args array.

In it, we call forEach with a callback to log the args entries.

Then we call func with all the entries in values as arguments with

func(...values);

We can also call it with the arguments written explicitly like

func(1, 2, 3);

Conclusion

To send a variable number of arguments to a JavaScript function, we can use the rest syntax.

Categories
JavaScript Answers

How to remove query string from URL with JavaScript?

Sometimes, we want to remove query string from URL with JavaScript.

In this article, we’ll look at how to remove query string from URL with JavaScript.

How to remove query string from URL with JavaScript?

To remove query string from URL with JavaScript, we can use the URL constructor.

For instance, we write

const getPathname = (path) => {
  return new URL(`http://_${path}`).pathname;
};

const pathname = getPathname("/foo/bar?baz=5");

to create a URL object with the http://_${path} to get the URL object.

Then we get the part of the URL before the query string with the pathname property.

Conclusion

To remove query string from URL with JavaScript, we can use the URL constructor.

Categories
JavaScript Answers

How to convert integer into its character equivalent with JavaScript?

Sometimes, we want to convert integer into its character equivalent with JavaScript.

In this article, we’ll look at how to convert integer into its character equivalent with JavaScript.

How to convert integer into its character equivalent with JavaScript?

To convert integer into its character equivalent with JavaScript, we can create a alphabetic string and use the number as the index of the character to get from the string.

For instance, we write

const char = "abcdefghijklmnopqrstuvwxyz"[code];

to get the letter from the "abcdefghijklmnopqrstuvwxyz" with code as the index.

Then we assign the letter string to char.

Conclusion

To convert integer into its character equivalent with JavaScript, we can create a alphabetic string and use the number as the index of the character to get from the string.