Posts

Showing posts with the label Javascript

Using an async iterator on Node.js + S3

There isn't support for  async iterators ( for await...of ) in Node.js v8.9 which is AWS Lambda's runtime. It's shame, as it’s a great feature that allows you to iterate over an iterable that returns as result asynchronously, i.e. retrieving another page from a database, using a compact for loop that feels synchronous but under the covers is actually done asynchronously. Which means, if you want to use a library that written specifically to use it (e.g.  Amazon DynamoDB QueryPaginator ), you have to use an even more verbose syntax. However with a bit of re-purposing you can use a generator function that returns a Promise and if you await each promise given in the loop it will behave like an async iterator.

Using web component polyfills with template tags

I've been playing around with using <template> tags  and how well they work with the current Web Component ( Custom Elements ) polyfills. My main motivation for going for Web Components instead of something like React or Angular is that I'm currently developing a chrome extension. I wanted the code base to be as small so that it didn't slow down devtools and increase the frequency of hands. Plus I think it's going to be the natural progression from the current React/Angular/etc components - especially with HTTP 2.0's server push of dependant files removing the need for tools like webpack by allow all dependant files to be automatically sent in response to one request. I immediately hit problems using custom elements in a chrome extension as they're disabled by default. So in order to use them I had to forcefully polyfill the existing API, it took a bit of fiddling  but now works with both libraries I looked at. Next, using template tags an import li...

How to chain an ES6 Promise

Node.js uses async functions extensively, as it based around non-blocking I/O. Each function takes a callback function parameter, which can result in some messy, deeply nested callback functions if you have to call a bunch of async functions in sequence. Promises make these callbacks a lot cleaner. ES6 (or ES2016)  Promise s are a great way of chaining together asynchronous functions so that they read like a series of synchronous statements. There's already some great posts on Promises, 2ality has a good intro to async then detail of the api , so I won't rehash that article. However, after starting to use them for cases more complicated than most examples, it easy to make a few mistaken assumptions or make things difficult for yourself. So here is a more complicated example showing a pattern I like to follow. In this example, I'll use the new Javascript Fetch API , which is an API that returns a Promise, allowing you to make async HTTP calls without having to muck a...

Javascript (ECMAScript 5.1) Refresher

I’ve been recently working heavily on JavaScript based applications in both Node.js and a Chrome extension. After working mostly on Java, I thought I’d share the syntax, conventions and the new features of ECMAScript 5.1 and parts of the still pending version, ES6 /ECMAScript 2015, that I’ve come across. Comparisons Operators == equal to === equal value and equal type != not equal !== not equal value or not equal type I saw something else I thought was a fancy notation: if (!!something) //which is not a special operator, it just converts it to boolean, i.e a double not. (!!something) === (!(!something)) //this would only be useful if you want to set a boolean to something truthy, i.e. var a = "somevalue"; var asBoolean = !!"somevalue"; Object Initializer Something I had thought was not a fully adopted syntax, was being able to specify the getter / setter functions in the initializer. (MDN Object initializer ) var o = { func1: function (p1, p2) {}, ge...

Safari removed javascript API

In the latest version of Safari (WebKit) they have removed some deprecated javascript API & tags which can cause problems with older code. This is my attempt to keep track of them so I can search through the code and change it to use a newer method. Here is a list of what works in Firefox 3 or Internet Explorer but not Safari: document.[form name] where [form name] matches the element <form name='[form name]'> Replacement: Use document.getElementById() instead document.embeds Replacement: Use document.getElementById() instead Form element's id defaulted to their name Replacement: Manually specify the id <applet> tag Replacement: <object> tag That's it for the moment but I'll keep updating the list as I find them.