Skip to content

Instantly share code, notes, and snippets.

View RinatMullayanov's full-sized avatar

Rinat Mullayanov RinatMullayanov

View GitHub Profile

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 / atom.txt
Created May 6, 2015 06:51
Atom: Unable to install packages behind corporate proxy https://github.com/atom/apm/issues/322
Seems that node-gyp isn't following the 302 Redirect sent by atom.io.
@fujisaks Thanks for pointing to the problem!
I've been able to workaround the issue by setting the ATOM_NODE_URL env variable to the new url (after redirect ). This should also be working after you update Atom. However, remove the entry when the bug gets fixed!
Windows temporary:
set ATOM_NODE_URL=http://gh-contractor-zcbenz.s3.amazonaws.com/atom-shell/dist
Windows permanently:
@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
@RinatMullayanov
RinatMullayanov / native_promise_sample.js
Last active September 18, 2015 11:26
Notes about promises
function updateTask(oldTask, newTask) {
var promise = new Promise(function (resolve, reject) {
resolve({status: 'success'});
// resolve(Error('some error'));
});
return promise;
}
<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);