Skip to content

Instantly share code, notes, and snippets.

View RinatMullayanov's full-sized avatar

Rinat Mullayanov RinatMullayanov

View GitHub Profile
<html>
<head>
<title></title>
</head>
<body>
Hi
<script>
var link = document.createElement('link')
link.setAttribute('rel', 'stylesheet')
link.setAttribute('href', require('path').join(__dirname, 'css', 'app.css'))
@RinatMullayanov
RinatMullayanov / aggregate.js
Last active August 29, 2015 14:21
Mongo Snippet
/*
> for(var i = 0; i < 100000; i++ ) {
db.sample.insert({"value": Math.random(1000) + 1, "date": new Date() });
}
> db.sample.findOne()
{
"_id" : ObjectId("5560cd3bf32e638da3683b38"),
"value" : 1.3218692857772112,
"tag" : "pressure"

However, promisey code is still hard to read, because promises are basically a bolt-on replacement for language primitives like try, catch, and return:

var db = new PouchDB('mydb');
db.post({}).then(function (result) { // post a new doc
  return db.get(result.id);          // fetch the doc
}).then(function (doc) {
  console.log(doc);                  // log the doc
}).catch(function (err) {
  console.log(err);                  // log any errors
});
// Promise.all is good for executing many promises at once
Promise.all([
promise1,
promise2
]);
// Promise.resolve is good for wrapping synchronous code
Promise.resolve().then(function () {
if (somethingIsNotRight()) {
throw new Error("I will be rejected asynchronously!");

I said before that the magic of promises is that they give us back our precious return and throw. But what does this actually look like in practice?

Every promises gives you a then() method (or catch(), which is just sugar for then(null, ...)). Here we are inside of a then() function:

somePromise().then(function () {
  // I'm inside a then() function!
});

What can we do here? There are three things:

@RinatMullayanov
RinatMullayanov / iterate.js
Created May 3, 2015 08:00
Right using for..in for iterate array. From http://habrahabr.ru/post/247857/
// a - разреженный массив
var a = [];
a[0] = "a";
a[10] = "b";
a[10000] = "c";
for (var key in a) {
if (a.hasOwnProperty(key) &&
/^0$|^[1-9]\d*$/.test(key) &&
key <= 4294967294) {
console.log(a[key]);
@RinatMullayanov
RinatMullayanov / mixins.ts
Created May 2, 2015 18:11
Mixins in Typescript
// Disposable Mixin
class Disposable {
isDisposed: boolean;
dispose() {
this.isDisposed = true;
}
}
//Activatable Mixin
class Activitable {
@RinatMullayanov
RinatMullayanov / Using Node.js with TypeScript.txt
Last active August 29, 2015 14:20
Using Node.js with TypeScript
1. Install TypeScript Definition manager for DefinitelyTyped https://github.com/DefinitelyTyped/tsd
2. Install required packages:
tsd install node --save
3. Add in top your .ts file /// <reference path="../../typings/node/node.d.ts" />
or all definition typed /// <reference path="../../typings/tsd.d.ts" />
4. Add flags in tsc
--module commonjs
<div class="main">
<!--flex block-->
<div class="item">1 Lorem ipsum dolor sit amet, consecteur adipisicing elit. Excepturi repellat nisi ad soluta vitae velit similique consequatur aliquam, facere ea.</div>
<!--flex block-->
<div class="item">2 Lorem ipsum dolor sit amet, consecteur adipisicing elit. Excepturi repellat nisi ad soluta vitae velit similique consequatur aliquam, facere ea.</div>
<!--flex block-->
<div class="item">3 Lorem ipsum dolor sit amet, consecteur adipisicing elit. Excepturi repellat nisi ad soluta vitae velit similique consequatur aliquam, facere ea.</div>
<!--flex block-->
<div class="item">4 Lorem ipsum dolor sit amet, consecteur adipisicing elit. Excepturi repellat nisi ad soluta vitae velit similique consequatur aliquam, facere ea.</div>
<!--flex block-->
@RinatMullayanov
RinatMullayanov / provider_factory_service_Angular_source_code.js
Last active August 29, 2015 14:19
https://github.com/angular/angular.js/blob/v1.3.15/src/auto/injector.js#L651 Provider is primary. Factory is wrapper over provider. Service is wrapper over factory.
////////////////////////////////////
// $provider
////////////////////////////////////
function supportObject(delegate) {
return function(key, value) {
if (isObject(key)) {
forEach(key, reverseParams(delegate));
} else {
return delegate(key, value);