๐ŸŒ Function sharing across files


โ€‹

โ€‹

โ€‹

Have you built a Webflow project with WF Localization? Is it working for you? Slater support for WF Localization has been a common request, so we now support it. If you have a Webflow project with Localization, give it a try.

Next, we are exploring how to support code sharing across files. Currently, we are binding functions to the window object, but that solution has some downsides. With the work we are doing on the next version of edgarallan.com, function sharing across files is becoming increasingly important to us.

Let's take a look at some new scripts in the community library.

Community library scripts

โ›“๏ธ Bind function to window objectโ€‹
โ€‹
Defines an empty global function to use in separate files. This is the current function sharing solution we use.

๐Ÿ›Ÿ Save data to localStorageโ€‹
Set localStorage Store an array of surfer names in localStorage as a JSON string.

๐ŸŽ Retrieve data from localStorageโ€‹
Retrieve and parse surfers data from localStorage.

โ€‹

--

Javascript 101: Fetch Requests to Greenhouse

Last week, we looked at code written by Ben Parker to display Jobs coming from the Greenhouse API. Let's spend more time with his code. Here is the code if you want to review it again.

Understanding how to handle network requests and asynchronous operations can be daunting. In this Javascript 101, we will explore Fetch API, Promises, and Error Handling through the Greenhouse Jobs API.

Fetch API: The Modern Way to Make Requests

JavaScript's Fetch API is a powerful tool for making network requests. It allows you to request resources from a server, like fetching data from an API. The Fetch API is straightforward to use and returns a promise, making it a great way to handle asynchronous operations.

Here we fetch a list of departments from a company's Greenhouse API:

const fetchData = (url) =>
fetch(url)
.then((response) => {
if (!response.ok) {
throwError(` ${response.status} ${response.statusText}`);
}
return response.json();
});
โ€‹

In this function, we use fetch(url) to make an HTTP GET request to the specified URL. The fetch function returns a promise, which represents the ongoing operation of fetching the resource.

Promises: Managing Asynchronous Operations

Promises are a fundamental concept in JavaScript for handling asynchronous operations. They provide a way to work with values that may not be available yet, like data fetched from a server.

A promise can be in one of three states:

  • Pending: The initial state, neither fulfilled nor rejected.
  • Fulfilled: The operation completed successfully.
  • Rejected: The operation failed.

When you call fetch, it returns a promise that will be fulfilled when the response is available. We handle the response using the then method:

fetchData('https://api.example.com/departments')
.then((data) => {
data.departments.forEach((department) => {
if (department.jobs.length > 0) {
// Do something with departments that have jobs
}
});
})
.catch(console.error)
.finally(writeJobs);
โ€‹

In this example:

  • We call fetchData with the URL of the API endpoint.
  • The then method processes the data once the promise is fulfilled. Here, we loop through the departments and do something with the ones that have jobs.
  • The catch method handles any errors that occur during the fetch or data processing.
  • The finally method runs the writeJobs function after the promise is settled, regardless of whether it was fulfilled or rejected.

Error Handling: Making Your Code Robust

Error handling is crucial to ensure your code can gracefully handle unexpected situations, like network failures or invalid responses.

In our fetchData function, we check if the response is okay using response.ok. If it's not, we throw an error with the status and status text:

constfetchData = (url) =>
fetch(url)
.then((response) => {
if (!response.ok) {
throwError(` ${response.status} ${response.statusText}`);
}
return response.json();
});
โ€‹

This error is then caught in the catch block when we call fetchData:

fetchData('https://api.example.com/departments')
.catch(console.error);
โ€‹

By throwing an error when the response is not okay, we ensure that our code can handle problems like network issues or server errors appropriately.

Understanding the Fetch API, Promises, and Error Handling is essential for working with asynchronous operations in JavaScript. The Fetch API allows you to make network requests easily, promises help manage asynchronous data, and proper error handling ensures your code can handle unexpected situations.

By understanding these concepts, you'll be well-equipped to handle more other APIs and share data across services.

โ€‹

--

Your projects, supported by Slater

Long-time Slater friend, Jeff McAvoy | The Rive Guy, is running a class called Interactive Animation with Rive. Check it out.โ€‹

โ€‹

Happy coding!

๐Ÿค™ the Slater Team

โ€‹

If Slater helps you create better websites, please support the team behind it.

Welcome To Slater!

Slater resources, updates and community activity

Read more from Welcome To Slater!
photo

Do you get confused when undoing (cmd + z) in Slater? We auto-format your code on save. The auto-formatting updates your code, and those updates get pushed into the undo stack. You can now improve your workflow by formatting the code when you chooseโ€”not just on save. To gain more control over when your code formats: Turn off auto-format code in the Code Editor Settings Use cmd + shift + f to format your code We added a handful of other improvements: Console logs and debuggers are now...

video preview

Last week, we announced that CSS updates were on the wayโ€”and now they're here! You can now generate CSS files just as quickly and easily as JS files. In the video below, we give a quick demo of the new functionality. If you have a Smart Script loaded in your project, you can instantly add CSS. In the demo, we also show how to add CSS in the Webflow designer. Give it a try and let us know what you're building! Community Library ๐Ÿ–๏ธ Text Highlight ColorCSS code for custom text selection. ๐Ÿ…ฐ...

Do you use Slater for CSS? CSS support in Slater has always been somewhat of an afterthoughtโ€”JavaScript is our love language! But sometimes, you need a simple way to add CSS to your project. Maybe you want to write a complex grid or use a CSS property that Webflow doesn't support. That's why we've decided to make CSS a first-class citizen. We considered launching the new CSS functionality today, but we want to test it a bit more. Expect it early next week. To prepare for the launch, let's...