Welcome. Here you find latest freeware and legal software as well as latest info about IT Technology.
Showing posts with label Toptal. Show all posts
Showing posts with label Toptal. Show all posts
Testing representational state transfer (REST) APIs can be a tricky business. Many who resort to manually testing REST APIs overlook details and miss out on ways they could save time.
Any REST API service being tested properly will have these areas covered:
  • Testing of whether the API service complies with REST standards
  • Testing of API service response codes
  • Assertion of API service response content
  • Performance benchmarking of API service
Tools for REST test
For this, you need the right tools. Apache JMeter and REST Assured are two popular REST test tools for testing REST API services. You can learn more about how you can use them in practice from here and here.
In this article, you will learn the pros and cons of both of these tools and where they fit best in your existing REST API projects.
One of the keys to writing a successful web application is being able to make dozens of AJAX calls per page.
This is a typical asynchronous programming challenge, and how you choose to deal with asynchronous calls will, in large part, make or break your app, and by extension potentially your entire startup.
Synchronizing asynchronous tasks in JavaScript was a serious issue for a very long time.
This challenge is affecting back-end developers using Node.js as much as front-end developers using any JavaScript framework. Asynchronous programming is a part of our everyday work, but the challenge is often taken lightly and not considered at the right time.

A Brief History of Asychronous JavaScript

The first and the most straightforward solution came in the form of nested functions as callbacks. This solution led to something called callback hell, and too many applications still feel the burn of it.
Then, we got Promises. This pattern made the code a lot easier to read, but it was a far cry from the Don’t Repeat Yourself (DRY) principle. There were still too many cases where you had to repeat the same pieces of code to properly manage the application’s flow. The latest addition, in the form of async/await statements, finally made asynchronous code in JavaScript as easy to read and write as any other piece of code.
Let’s take a look at the examples of each of these solutions and reflect on the evolution of asynchronous programming in JavaScript.
To do this, we will examine a simple task that performs the following steps:
  1. Verify the username and password of a user.
  2. Get application roles for the user.
  3. Log application access time for the user.

Approach 1: Callback Hell (“The Pyramid of Doom”)

The ancient solution to synchronize these calls was via nested callbacks. This was a decent approach for simple asynchronous JavaScript tasks, but wouldn’t scale because of an issue called callback hell.
Illustration: Asynchronous JavaScript callback hell anti-pattern
The code for the three simple tasks would look something like this:
const verifyUser = function(username, password, callback){
   dataBase.verifyUser(username, password, (error, userInfo) => {
       if (error) {
           callback(error)
       }else{
           dataBase.getRoles(username, (error, roles) => {
               if (error){
                   callback(error)
               }else {
                   dataBase.logAccess(username, (error) => {
                       if (error){
                           callback(error);
                       }else{
                           callback(null, userInfo, roles);
                       }
                   })
               }
           })
       }
   })
};
Each function gets an argument which is another function that is called with a parameter that is the response of the previous action.
Too many people will experience brain freeze just by reading the sentence above. Having an application with hundreds of similar code blocks will cause even more trouble to the person maintaining the code, even if they wrote it themselves.
This example gets even more complicated once you realize that a database.getRoles is another function that has nested callbacks.
const getRoles = function (username, callback){
   database.connect((connection) => {
       connection.query('get roles sql', (result) => {
           callback(null, result);
       })
   });
};
In addition to having code that is difficult to maintain, the DRY principle has absolutely no value in this case. Error handling, for example, is repeated in each function and the main callback is called from each nested function.
More complex asynchronous JavaScript operations, such as looping through asynchronous calls, is an even bigger challenge. In fact, there is no trivial way of doing this with callbacks. This is why JavaScript Promise libraries like Bluebird and Q got so much traction. They provide a way to perform common operations on asynchronous requests that the language itself doesn’t already provide.
That’s where native JavaScript Promises come in.

JavaScript Promises

Promises were the next logical step in escaping callback hell. This method did not remove the use of callbacks, but it made the chaining of functions straightforward and simplified the code, making it much easier to read.
Illustration: Asynchronous JavaScript Promises diagram
With Promises in place, the code in our asynchronous JavaScript example would look something like this:
const verifyUser = function(username, password) {
   database.verifyUser(username, password)
       .then(userInfo => dataBase.getRoles(userInfo))
       .then(rolesInfo => dataBase.logAccess(rolesInfo))
       .then(finalResult => {
           //do whatever the 'callback' would do
       })
       .catch((err) => {
           //do whatever the error handler needs
       });
};
To achieve this kind of simplicity, all of the functions used in the example would have to be Promisified. Let’s take a look at how the getRoles method would be updated to return a Promise:
const getRoles = function (username){
   return new Promise((resolve, reject) => {
       database.connect((connection) => {
           connection.query('get roles sql', (result) => {
               resolve(result);
           })
       });
   });
};
We have modified the method to return a Promise, with two callbacks, and the Promise itself performs actions from the method. Now, resolve and reject callbacks will be mapped to Promise.then and Promise.catch methods respectively.
You may notice that the getRoles method is still internally prone to the pyramid of doom phenomenon. This is due to the way database methods are created as they do not return Promise. If our database access methods also returned Promise the getRoles method would look like the following:
const getRoles = new function (userInfo) {
   return new Promise((resolve, reject) => {
       database.connect()
           .then((connection) => connection.query('get roles sql'))
           .then((result) => resolve(result))
           .catch(reject)
   });
};

Approach 3: Async/Await

JavaScript is asynchronous by default. This might be reason why it took so long to get synchronous-looking code that runs properly in JavaScript. But, better late than never! The pyramid of doom was significantly mitigated with the introduction of Promises. However, we still had to rely on callbacks that are passed on to .then and .catch methods of a Promise.
Promises paved the way to one of the coolest improvements in JavaScript. ECMAScript 2017 brought in syntactic sugar on top of Promises in JavaScript in the form of async and await statements.
They allow us to write Promise-based code as if it were synchronous, but without blocking the main thread, as this code sample demostrates:
const verifyUser = async function(username, password){
   try {
       const userInfo = await dataBase.verifyUser(username, password);
       const rolesInfo = await dataBase.getRoles(userInfo);
       const logStatus = await dataBase.logAccess(userInfo);
       return userInfo;
   }catch (e){
       //handle errors as needed
   }
};
Awaiting Promise to resolve is allowed only within async functions which means that verifyUser had to be defined using async function.
However, once this small change is made you can await any Promise without additional changes in other methods.

Async - A Long Awaited Resolution of a Promise

Async functions are the next logical step in the evolution of asynchronous programming in JavaScript. They will make your code much cleaner and easier to maintain. Declaring a function as async will ensure that it always returns a Promise so you don’t have to worry about that anymore.
Why should you start using the JavaScript async function today?
  1. The resulting code is much cleaner.
  2. Error handling is much simpler and it relies on try/catch just like in any other synchronous code.
  3. Debugging is much simpler. Setting a breakpoint inside a .then block will not move to the next .then because it only steps through synchronous code. But, you can step through await calls as if they were synchronous calls.
Software rarely, if at all, exists in an informational vacuum. At least, that is the assumption we software engineers can make for most of the applications we develop.
At any scale, every piece of software—in one way or another—communicates with some other software for various reasons: to get reference data from somewhere, to send monitoring signals, to be in touch with other services while being a part of a distributed system, and more.
Apache Camel for Streamlined Software Integration
In this tutorial, you will learn what some of the biggest challenges of integrating large software are and how Apache Camel solves them with ease.
The AI revolution is already transforming the consumer world.
Sometimes, it’s in everyday ways like product recommendations, and sometimes it’s in magnificent ways: Cochlear implants, which provide artificial hearing for those born completely deaf, have switched to AI for a superior end-user experience.
Artificial intelligence (AI) is the latest milestone in modern technology.
The AI revolution is leading to a smarter world, and this smarter world has been built on the mega-trends that we’ve all witnessed over the last 20 years: the web, the cloud, social, mobile, and the Internet of Things (IoT).

Powered by Blogger.