The ES6 Promise specification contains the very useful constructor for creating a promise, like so:
var promise = new Promise(function(resolve, reject){
resolve("hi");
});
promise.then(function(message){
console.log(message);
});The ES6 Promise specification contains the very useful constructor for creating a promise, like so:
var promise = new Promise(function(resolve, reject){
resolve("hi");
});
promise.then(function(message){
console.log(message);
});JavaScript has labels, which can be placed before blocks to create a named section of code, serving almost the same use as an imidiately invoked function expression (IIFE)
(function iife(){
var scoped = "this is scoped stuff";
})();
iife: {
let scoped = "this is scoped";
}Currently you can export things from a module in at least six different ways, and import things in at least six different ways, resulting in 36 different combinations, most of which are not semantically valid.
Here is a greatly simplified (and probably naive) suggestion for modules in ES6:
###export You can only export named things, including variables and functions.
let a = "hello";
export a;| //The file foobar.js contains these two lines of code | |
| var foo = "foo", bar = "bar"; | |
| export default = {foo, bar}; | |
| //It can now be imported into another module using either of these two lines: | |
| import foobar from "foobar"; | |
| module foobar from "foobar"; | |
| //But not this line: | |
| import {foo, bar} from "foobar"; |
| module _ from "underscore"; | |
| import _ from "underscore"; | |
| import module _ from "underscore"; | |
| import {each, map, find} from "underscore"; | |
| import "underscore"; | |
| import default _ from "underscore"; | |
| const _ = System.import("underscore"); | |
| import "underscore" as _; | |
| import {_} from "underscore"; | |
| import {each as forEach} from "underscore"; |
| var component1 = { | |
| //this method will receive the event and process it | |
| handleEvent: function(event){ | |
| //this is only for debugging | |
| console.log("debugging!", event.data); | |
| //the event causes the state of the component to change |
| define([], function() { | |
| return function deadWeight() { | |
| var weight = 1000000; | |
| var theWeight = new Array(weight); | |
| for (var i = 0; i < weight; i++) | |
| theWeight[i] = i; | |
| this.getWeight = function() { return theWeight;} | |
| } | |
| }); |
| /* | |
| This is how 3 dependencies are defined using AMD. | |
| Notice how when the number of dependencies grows the | |
| distance between the path to the dependency and the | |
| variable it is stored in grows. It quickly becomes | |
| difficult to see which variable corresponds to which | |
| module name. | |
| */ | |
| define(["jQuery", "an/other/module", "knockout"], function($, dep, ko){ |
| var a = 5; | |
| function whatIsA() { | |
| if(a == undefined) { | |
| var a = 7; | |
| } | |
| return a; | |
| } | |
| whatIsA()//is it 5 or 7? |