Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now
Build the future of communications.
Start building for free
  • By Maciej Treder
    Asynchronous JavaScript: Using Promises With REST APIs in Node.js async-js-apis-nodejs.png

    In JavaScript, like many programming languages, asynchronous processing can be used to handle tasks that take a long time to run or are potentially unresponsive. The JavaScript Promise object provides a way of monitoring their state and serves as a placeholder and container for the data they’ll eventually return — or not.

    Often it doesn’t matter when a Promise returns data and the members of a collection of Promises can be resolved independently without regard to timing. A function can perform asynchronous tasks that don’t depend on the data from, or the success of, other asynchronous tasks.

    But sometimes business rules or program design require that asynchronous actions are dependent on other asynchronous operations. For example:

    • Establishing a remote database connection must be preceded by loading configuration information from the file system.
    • Performing a REST API call to a weather API must be preceded by obtaining the user’s location from …
    Read More
  • By Maciej Treder
    Asynchronous JavaScript: Introducing async and await async-js-async-await.png

    Asynchronous processing is one of the most important aspects of the JavaScript language. JavaScript is a single-threaded language, so it needs a mechanism to deal with the actions like performing time-intensive and potentially unreliable activities like making REST API calls, reading files from the disk, or interacting with user input.

    In JavaScript the mechanism used is an event loop, which processes a callback queue in order from oldest item to newest. Each item in the queue is a message that’s processed completely before moving on to the next next message. Each message is a function that returns a response to code elsewhere in the application. Functions used in this way are called callback functions.

    Because of callbacks, JavaScript can use a number of techniques for getting around its single-threaded nature to implement asynchronous processing. Unfortunately, some of those techniques, JavaScript Promises and RxJS Observables, force you to introduce at …

    Read More
  • By Maciej Treder
    COVID-19 Diversions: Tracking the ISS with Real-Time Event Notifications Using Node.js, RxJS Observables, and Twilio Programmable SMS tracking-iss-rxjs-observables.png

    Are you a developer looking for a new hobby that’s compatible with sheltering in place? Would you like to be able to share it with friends from a virus-safe distance?

    You can stargaze from the safety of your own lawn, balcony, or roof, and you don’t need any equipment other than your own eyes, and maybe a pair of binoculars. There are plenty of bright objects in the night sky that are visible with the naked eye. One of them is the International Space Station (ISS), which is the third brightest object in the sky after the sun and moon.

    The ISS whips around the earth at 27,576 kilometers per hour (17,000 miles per hour), making a complete orbit every 94 minutes. Opportunities to observe it only last a few minutes, so it’s helpful to know when the ISS will be visible and from which direction it will appear.

    You …

    Read More
  • By Maciej Treder
    Confirming SMS Message Delivery with RxJS Observables, Node.js, and Twilio Programmable SMS confirming-sms-message-delivery-rxjs.png

    SMS messages can reach over 4.5 billion text-enabled devices to notify people about upcoming appointments, emergencies, traffic disruptions, or commercial promotions. Sending SMS messages programmatically enables you to reach many people almost simultaneously. With Twilio Programmable SMS you can create a Node.js application that sends many messages and reports when each of them has been delivered—or not.

    Twilio Programmable SMS includes a helper library for Node.js that makes it easy to interact with the SMS API without having to create and manipulate connections to the API endpoints. Once an SMS message request is created with the helper library its status is emitted as a JavaScript Promise. While that is great for creating a request asynchronously, Promises only resolve once, and the message request will pass through more than one state as it makes its way—or not—to the recipient.

    You can create a mechanism for monitoring and reporting the status …

    Read More
  • By Maciej Treder
    Asynchronous JavaScript: Using RxJS Observables with REST APIs in Node.js Async JS RxJS Observables Node js

    ReactiveX is an electrifying programming concept. It’s widely adopted in popular programming languages, including JavaScript. ReactiveX programs can react to data as it is emitted from a source, rather than get the data from it. This is a convenient way of handling data from sources like web APIs or WebSockets.

    In this post you’ll get hands-on experience doing ReactiveX programming with RxJS: ReactiveX for JavaScript. You’ll learn how to perform REST API calls to retrieve data asynchronously, manipulate it as it arrives, and perform subsequent calls based on the emitted data. You’ll also see how to perform other actions whenever data is emitted by an Observable.

    This post focuses on how to utilize RxJS Observables with REST API calls. The previous post in this series on Asynchronous JavaScript explains RxJS fundamentals:

    Understanding the case study project

    In this post you will see how …

    Read More
  • By Maciej Treder
    Asynchronous JavaScript: Introducing ReactiveX and RxJS Observables async-js-reactivex-rxjs-observables.png

    Reactive Extensions (ReactiveX) are one of the most widely-adopted set of tools in programming today. ReactiveX libraries for JavaScript and other languages provide a way to operate on sequences of data as if they were events. This is a convenient way to handle data from asynchronous sources like web APIs: your programs can react to data as it is emitted from the source without tightly coupling the code that uses the data with the code that gets the data.

    In this post you’ll learn about the fundamental concepts of ReactiveX programming and get hands-on experience doing ReactiveX programming with RxJS for JavaScript. You’ll learn how to retrieve data asynchronously, manipulate it as it arrives, and handle errors. You’ll also see how RxJS Observables compare to JavaScript Promises.

    Introducing the ReactiveX API and the RxJS library

    Reactive Extensions, known as ReactiveX, is an application programming interface (API) enabling programming languages …

    Read More
  • By Maciej Treder
    Asynchronous JavaScript: Refactoring Callbacks to Promises in Node.js Advanced JavaScript: Refactoring Callbacks to Promises

    If you know how the event loop mechanism works in JavaScript you know how it enables you to work with asynchronous events. You might also know how to refactor your code into separate functions to reduce the amount of nesting associated with a sequence of callback functions. If you work with web APIs you are probably familiar with the JavaScript request library, which is used to perform HTTP calls.

    You can put these skills together to retrieve remote data through an HTTP request and work with the call response asynchronously. But in doing so you’ll probably notice that code has at least one pitfall: the logical order of declared functions is reversed. That makes the code hard to read and maintain.

    With the node-fetch JavaScript library and Promise objects you can reduce nesting, known as the callback “Pyramid of Doom”, and organize your code in a more readable and logical …

    Read More
  • By Maciej Treder
    Asynchronous JavaScript: Advanced Promises with Node.js Asynchronous JavaScript: Advanced Promises with Node.js

    JavaScript Promise objects are a considerable improvement over basic JavaScript callback functions. Promises provide an object that represents both the state of an asynchronous function and the values returned by it. The state indicates whether the promise is pending, fulfilled by a successfully completed operation, or rejected because of an error or other condition. When a promise is fulfilled its value is determined by the asynchronous operation executed by the promise.

    Promises can also be chained together to use the state of settled promises (resolved or rejected) and the promises’ values to control the flow of execution in your program. This is useful when you have a sequence of asynchronous operations to perform that each depend on the results of the previous operation: the next step in the chain isn’t performed until the preceding step is resolved. Chains of promises also include error propagation; errors in a …

    Read More
  • By Maciej Treder
    Asynchronous JavaScript: Introduction to JavaScript Promises Asynchronous JavaScript Introduction to Promises

    Asynchronous processing is one of the most important capabilities JavaScript acquired as the language matured. JavaScript’s async capabilities enable more sophisticated and responsive user interfaces. They also make it possible to implement a distributed web application architecture built on standards like SOAP and REST.

    JavaScript Promises are currently the most powerful and flexible asynchronous technology built into the language itself. This post will explain how they work and get you writing your own promises with some practical examples.

    Promises offer a number of advantages:

    • a promise can be called multiple times
    • promises can be chained together
    • promises can include error rejection events
    • errors in a chain of promises need only be handled once (error propagation)
    • promises can be used to wrap old-style callback functionality
    • promises always execute after the current execution stack is empty

    If you’re just starting out with asynchronous functionality in JavaScript the preceding points might not mean …

    Read More
  • By Maciej Treder
    Asynchronous JavaScript: Organizing Callbacks for Readability and Reusability cxgwWEzJkPC0ZPM2fWcHu3BrGZXaMUO2Z0tUwOu9UTctbND9UrJpqwisJe7EOp9UZlf6sUZhhnH80lvOQadVfLyAfaswgFqm0BB9rs6FD9beeFkQYLMHSwNBo1kNwgkMMrBcHX4l

    Asynchronous programming is an essential part of being a complete JavaScript programmer. Understanding the code execution flow in JavaScript is the foundation of understanding how it can handle asynchronous tasks. And being able to program asynchronous tasks enables you to take advantage of the extensive array of functions provided by JavaScript runtime engines and external APIs. With those tools you can connect your JavaScript programs with web APIs across the internet and effectively manage those—sometimes tenuous—connections.

    With power comes complexity. Using JavaScript callbacks to implement asynchronous functionality can quickly create a series of deeply nested function calls. This post will show you how to write and organize your JavaScript callbacks to maximize the readability, maintainability, and reusability of your asynchronous functions.

    As an old programming saying goes:

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

    The first part of this …

    Read More
  • Newer
    Older
    Sign up and start building
    Not ready yet? Talk to an expert.