Development on Various Platforms
12.8K views | +2 today
Follow
Development on Various Platforms
Your new post is loading...
Your new post is loading...
Rescooped by Ertunç Efeoğlu from JavaScript for Line of Business Applications
Scoop.it!

A Simple Visual Model for Promises

A Simple Visual Model for Promises | Development on Various Platforms | Scoop.it

Promises are significantly taking ground as a programming construct for asynchronous systems such as web applications. In its simplest form, a promise represents the state of the result of some future computation.

I was introduced recently to this construct when studying jQuery and AngularJS. At the beginning, I was not able to wrap my head around it. While I understood the basic callback mechanism — allowing to be notified when the computation is finished — I struggled a lot capturing the efficiency with which promises allow to easily compose asynchronous programs.


Via Jan Hesse
more...
No comment yet.
Rescooped by Ertunç Efeoğlu from JavaScript for Line of Business Applications
Scoop.it!

Modern JavaScript: Promise patterns

Modern JavaScript: Promise patterns | Development on Various Platforms | Scoop.it

Promises are great for giving us a way of writing asynchronous code without having to indent our code but if that's the only thing you're using them for then you're missing the point of promises. Promises are an abstraction that make doing several things easier. They have two properties that make them easier to work with:

* You can attach more than one callback to a single promise

* values and states (errors) get passed along

Because of these properties it makes common asynchronous patterns using callbacks easy. Here are some cases which may pop up from time to time: 

* Converting callback functions to return promises

* Sequential Calls

* Error Handling

* Pool

* Some fun


Via Jan Hesse
more...
No comment yet.
Rescooped by Ertunç Efeoğlu from JavaScript for Line of Business Applications
Scoop.it!

ES6 Iterators, Generators, and Iterables

I wrote up a quick guide to the terminology around ES6's iteration-related concepts, plus some notes and other resources.

An iterator is an object with a next method that returns { done, value } tuples.

An iterable is an object which has an internal method, written in the current ES6 draft specs as obj[@@iterator](), that returns an iterator.

A generator is a specific type of iterator which also has a throw method, and whose nextmethod takes a parameter.

A generator function is a special type of function that always returns a generator, and which can use the special contextual keyword yield inside of itself. You can send values or exceptions into the body of the function, at the points where yield appears, via the returned generator's nextand throw methods. They are created with function* syntax.

A generator expression is a shorthand expression for creating generators


Via Jan Hesse
more...
No comment yet.
Rescooped by Ertunç Efeoğlu from JavaScript for Line of Business Applications
Scoop.it!

Designing APIs for Asynchrony

Designing APIs for Asynchrony | Development on Various Platforms | Scoop.it

Personally, I don’t actually care all that much about the whole promises/callbacks/coroutines/generators debate. It’s provably trivial to turn any callback-taking API into a promise-returning API or generator-yielding API or vice-versa.

I’m of the opinion that breaking functionality into small enough chunks that your boilerplate doesn’t matter is much more important than which boilerplate you decide tastes the yummiest. In JavaScript, we have first-class language support for passing functions around, so I tend to use callbacks, simply because it requires no extra “stuff”.

What’s important to me is that an API be useful, performant, and easy to reason about. When I’m writing JavaScript, I care a lot about how V8 is going to optimize it, and so try to choose patterns that it can optimize nicely. As of today, generators just aren’t first-class enough, and Promises require an extra abstraction. That may change someday, but these principles probably will not.


Via Jan Hesse
more...
No comment yet.
Rescooped by Ertunç Efeoğlu from JavaScript for Line of Business Applications
Scoop.it!

JavaScript Callbacks are Pretty Okay

JavaScript Callbacks are Pretty Okay | Development on Various Platforms | Scoop.it

I've seen a fair amount of callback bashing on Hacker News recently.

Among the many proposed solutions, one of them strikes me as particularly clean: "asynchronous wait" or "wait and defer". iced-coffee-script had this a while ago. Kal just debuted with an identical solution, only differing in syntax. Supposedly LiveScript supports this with "backcalls".

I must say, "asynchronous wait" or "monads" - whatever you want to call it - seems like an improvement over callbacks. But I'm here to say that actually... callbacks are pretty okay. Further, given how clean callbacks can be, the downsides of using a compile-to-js language often outweigh the benefits.

I have 2 rules of thumb for code organization which makes clean callback based async code brainless:

Avoid nontrivial anonymous functions.Put all function declarations after the code that actually does things.
Via Jan Hesse
more...
No comment yet.
Rescooped by Ertunç Efeoğlu from JavaScript for Line of Business Applications
Scoop.it!

Handling concurrency and asynchronous JavaScript

If you’ve worked with JavaScript in a browser, or indeed on the server, for any length of time you’ve probably had to deal with asynchronous spaghetti. This kind of spaghetti is not delicious in any way, shape or form.

You end up with multiple boolean variables and function calls constantly checking if all of those AJAX requests have finished; it is far from ideal. I’m going to show you how to break the problem down and come out of the other side with your sanity still intact.

The first step in explaining this involves creating a problem, here’s the scenario: You’re running a site like gist.github.com and you need to fetch some meta data for a list of gists from the server. This is going to involve you making multiple requests and then doing something when they’re all finished, you’ll probably want a loading spinner overlay or something while it fetches them as well.


Via Jan Hesse
more...
No comment yet.
Rescooped by Ertunç Efeoğlu from JavaScript for Line of Business Applications
Scoop.it!

Promises in JavaScript

Writing code in an async programming framework like node.js becomes super complex and unbearably ugly super fast. You are dealing with synchronous and asynchronous functions and mixing them together can get really tricky. That is why we have Control Flow.

Basically, control flow libraries allow you to write non-blocking code that executes in an asynchronous fashion without writing too much glue or plumbing code. There is a wholesection on control-flow modules to help you unwind the sphagetti of callbacks that you will quickly land yourself into while dealing with async calls in node.js.

Control flow as a pattern can been implemented via several approaches and one of such approaches is called Promises.

 


Via Jan Hesse
more...
No comment yet.
Rescooped by Ertunç Efeoğlu from JavaScript for Line of Business Applications
Scoop.it!

Generators vs Fibers

Generators vs Fibers | Development on Various Platforms | Scoop.it

Both ES6 generators and node-fibers can be used to block a coroutine while waiting on some I/O without blocking the entire process.

This means that you can wait for an I/O result within your code but still have the performance benefits of the single-threaded, non-blocking I/O model you all know and love.

Both can do this for arbitrarily deep call stacks. The main difference between the capabilities of the two is how explicit the syntax is. It's the classic battle between wanting to be safe while wanting to be flexible.

* Generators - Safe, but Annoyingly Explicit* Fibers - Powerful, but Flexible


Via Jan Hesse
more...
No comment yet.
Rescooped by Ertunç Efeoğlu from JavaScript for Line of Business Applications
Scoop.it!

Modern JavaScript: Promises/A+ - understanding the spec through implementation

Modern JavaScript: Promises/A+ - understanding the spec through implementation | Development on Various Platforms | Scoop.it

What we're going to do is create a promises/A+ implementation based on http://promises-aplus.github.io/promises-spec/. By doing this hopefully we'll get a deeper understanding of just how promises work.

according to the terminology a value can be anything including undefined and a reason is any value that indicates why a promise was rejected. That last definition is a little blurry - can "undefined" indicate why something was rejected? I'm going to say no and only accept non-null values. If anything doesn't work then I'll throw an error.


Via Jan Hesse
more...
No comment yet.
Rescooped by Ertunç Efeoğlu from JavaScript for Line of Business Applications
Scoop.it!

Deferred Objects

Deferred Objects | Development on Various Platforms | Scoop.it

A Deferred object is a mechanism for referencing a value which may (or may not) be available at a later point.

Every so often you might require a group of operations to complete before you continue. These operations might need to happen either in sequence or in any order, but they all have to finish before the code can continue.

Ajax requests are the best examples of Deferred Objects because they're so commonly used. But some other cases for Deferred Objects include file access, web workers or other asynchronous calls.

An interesting use I've toyed with is user interaction. Embedded below is an example that shows how to combine deferred objects with mouse events to create a simple mouse dexterity game.


Via Jan Hesse
more...
No comment yet.