Tagged: Promises

Object JavaScript – ECMAScript 6 Code Preview

imageECMAScript 6 specification and implementation is underway and promises to bring many of the features that you’ve learned about in the posts on Object JavaScript.

This post gives you an idea of what the code looks like in ECMAScript 6. This post doesn’t cover ever feature. But you will learn about how ECMAScript 6 relates to:

  • Scope
  • Arrow Functions and Lexical this
  • Default Function Parameters
  • Classes
  • Inheritance
  • Modules
  • for-of
  • Arrow functions
  • Promises

I won’t come close to covering all the features. But you can get an idea of how ECMAScript 6 works to support the idea of Object JavaScript. Look to the references and to the specification for information about: Parameter handling, multiple return values, collections, destructuring, rest parameters & spread operator, iterators, array comprehension, and more.

Special thanks to Axel Rauschmayer for many of the snippets.

Continue reading

Advertisements

Object JavaScript – ECMAScript 6 Futures Overview

imageECMAScript 6 specification and implementation is underway and promises to bring many of the features that you’ve learned about in the posts on Object JavaScript. ECMAScript is the foundation of JavaScript. ECMAScript has gone through several versions. The current browsers support ECMA Script 5. . The discussions today are revolving improvements beyond versions past 5, which are code-named, ECMAScript Harmony.

The ES6 compatibility table is very useful, as it tells us the ES6 features that are supported in the current browser. It also gives us a handy link to the specifications for each of the features listed.  You will find that the current versions of browsers are implementing these features as fast as they can. The table shows that some subset of the feature exists, so as we say,  “your mileage may vary”. That said, it is coming.

I don’t have any particular insider information, but wanted to share what I am learning as I explore ECMAScript 6 and what it means to the way that code is written today. In my search I found two great articles that I am pulling information from:

My value add is to provide context for the previous posts and show how your code in the future could look like to implement many of the same features. And this topic is fluid so again, “your mileage may vary”. My intent is to give you can idea of what is coming and how soon to help you decide how deeply you want to invest in the current technologies. That said, one of the goals in ECMAScript 6 is to not break anything you are doing now.

Continue reading

Single Page App – Asynchronous Sample Using jQuery Promise to Render JSON Using Mustache

6327_image_58FAEDFAIn the previous posts on promises Promises for Asynchronous Operations Using jQuery, you learned how you can build promises using jQuery Deferreds and Promises. And in External Templates Using Mustache, jQuery, you learned how to bring in an external template.

It is time to show a real life example of how this code comes together. And in doing so, we have the beginning for a Single Page App.

In this code example, you will see how to use jQuery Promises to:

  • Load some JSON data
  • Load a Mustache template
  • Build your own deferred object for your own long-running function

Then when all three are accomplished, you’ll use the jQuery $.when() function to render the data.

For this example, you will need to have jQuery and Mustache loaded in your Scripts folder. Continue reading

Object JavaScript – Using Q Promises Inside a RequireJS AMD Module

image613As you are thinking more about your Web page being an app, you look for ways to reduce the complexity by using modules. In earlier post Getting Started with Modules Using RequireJS , you learned how RequireJS provides a great way to think of your app in modules and to asynchronously load and run your app.

RequireJS helps your describe the dependencies of a module and make sure you load them before executing your script.

But what happens when your module is long running? You can certainly turn that portion into a module and the require the completion before continuing. But in my case, I want think about my AMD module as an object and then call long-running methods on that module after it has been loaded.

This snippet expands on Asynchronous JavaScript Promises Using Q  and shows how you can use a promise inside your module that will have some long running asynchronous method.

687474703a2f2f6b7269736b6f77616c2e6769746875622e696f2f712f712e706e67When you make an asynchronous call, you can use a promise to handle both successful completion of the work and potential errors that may arise during execution. Upon the successful completion of one asynchronous call, you may want to pass the result to make another request.

The solution combines the promises of Q.js with the Asynchronous Module Definition (AMD) of Require.JS.

Continue reading

Object JavaScript – Promises By Integrating Q with jQuery

6327_image_58FAEDFAAs you have seen in previous posts, a promise in JavaScript represents the result of a task, which may or may not have completed yet. Or in simpler words, what to expect for a JavaScript call.

Q was designed to provide a robust way to provide you ways to write asynchronous code cleanly.

You can integrate the robustness of Q with jQuery promises.

Continue reading

Object JavaScript – Asynchronous JavaScript Promises Using Q

687474703a2f2f6b7269736b6f77616c2e6769746875622e696f2f712f712e706e67Q is a library that implements the standard and has some extra helpers. Q works in the browser and in node.js.

Q was designed to provide a robust way to provide you ways to write asynchronous code cleanly.

If a function cannot return a value or throw an exception without blocking, it can return a promise instead. A promise is an object that represents the return value or the thrown exception that the function may eventually provide. A promise can also be used as a proxy for a remote object to overcome latency.

You can read the specifications for Q at Promises A+, which aims to clarify “the behavioral clauses of the Promises/A proposal, extending it to cover de facto behaviors and omitting parts that are underspecified or problematic.”

You use deferreds and promises in ways similar to the ways you would use them in jQuery. However, Q has some important features.

Continue reading

Object JavaScript – Promises for Asynchronous Operations Using jQuery

imageYou need promises as soon as you do anything that involves an asynchronous API. In Object JavaScript – Asynchronous Programming Using Promises, you learned the basics about promises.

jQuery’s implementation of promises is based around the jQuery.Deferred object. This is a chainable constructor where you can check for the existence of a promise. The jQuery Deferred object can also invoke callback queues and pass on the success of synchronous and asynchronous functions.

Continue reading

Object JavaScript – Asynchronous Programming Using Promises

10063_580983808600819_401360548_nPromises are a way that lets us write asynchronous code that is almost as easy to write as if it was synchronous.

You need promises as soon as you do anything that involves an asynchronous API. It also does not take very long before writing promise chains for sequential asynchronous operations becomes second nature.

A Promise is an object that basically represents a process that is or will take place at some point in time, but allows you to register callbacks to it for when the process gets terminated or completed.

Instead of blocking and waiting for the long-running computation to complete, a promise returns an object which represents the promised result.

In this post, you will get an introduction into JavaScript promises.

Continue reading