« first day (1645 days earlier)      last day (3307 days later) » 

8:00 PM
What did the farmer say when he couldn't find his tractor?
"Where's my tractor?"
 
Hangouts?
 
What did the tractor say when it couldn't find its farmer?
"..."
 
"Oh Deere"
 
Ok. I'm done with jokes for the day.
 
8:01 PM
take it from Dennis Ritchie
 
@KendallFrey His face kills me
 
that kills me
FEELS
 
@KendallFrey Your REBEL pgm is pretty cool :)
 
random, but thx
 
8:09 PM
Actually I was just trying to make you feel happy again after all those momma jokes.
 
Does anyone know of a good solution for using bower modules with browserify?
 
Your self-esteem should be destroyed by now.
 
whatever, momma jokes make me happy
I'm just bored as fuck right now
 
Now that I remember why I came here...
 
@KendallFrey vimeo.com/78419167
 
8:11 PM
@twiz NO
(Though seriously, substack just laughed when I mentioned Bower)
 
Yea, bower needs to just go away.
Libraries that aren't distributed with npm REALLY bother me...
 
I dare someone to get this right in the first time (without debugging it)
 
What would this return?
return computers.count({where:{domain:d}}).then(function(count){
  if(count>0){
    return true;
  }else{
    return false;
  }
});
 
A promise
 
Yup I need to learn that Promises stuff
 
8:21 PM
How can I make It return true or false
 
You can't
 
Where should I get a short reference / mini tutorial ?
 
Will the docs do?
 
No I'm noob
 
How can I make 'exists' return true or false?
  exists: function(d){
    return computers.count({where:{domain:d}}).then(function(count){
      if(count>0){
        return true;
      }else{
        return false;
      }
    });
  }
 
8:23 PM
@l0oky Well, you can make an educated guess and then verify asynchronously if you've got it right, hopefully improving your predictions for the next time
 
predictions?
 
Guesswork
 
haha :D
 
@l0oky You can't.
 
oh man that makes me sad :(
 
8:25 PM
Since exists is asynchronous, the best you can do is return Promise<Boolean>
 
If you need to do something asynchronously, you need the promise/callback. You can't just make it return true/false.
 
Promise<Boolean> ?
 
So later, you exists(something).then(bool) { ... }
@l0oky A promise for a boolean
 
IO Bool
 
A third .then? :D
 
8:26 PM
Perhaps
 
hmm that's so uncool
 
not really
 
I think I'm looking at Promises in a very limited way. In the MDN example, the Promise calls another function (.then) when the first one completes. Why not put it all together in the async function?
 
You're already returning a Promise<bool>
 
Well exists has to return a value with boolean info
 
8:28 PM
models.computers.exists(...).then(function(exists) { console.log(exists); });
 
but I am getting this as a return statement { _bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_progressHandler0: undefined,
_promise0: undefined,
_receiver0: undefined,
_settledValue: undefined,
_boundTo: undefined,
'$sql': [] }
 
Since .count(), like all DB queries is asyncrounous, exists is, too.
Yea, that's a promise.
 
oh
 
@l0oky That's unfortunate
 
@JanDvorak haha :DD
 
8:29 PM
call .then on it, exactly like you do inside of .exists()
 
			models.computers.exists('test').then(function(bool){
				assert.equal(true, bool);
			});
 
looks good
 
yup, but i'd use somethign other that 'bool'. name the variable by what it contains, not whta type it is
Fro the same reason a varaible named "string" or "integer" is not meaningful.
 
@Catgocat The idea of a Promise is that it represents a value that will be available in the future
 
Oh, that's why it is called promise?
 
8:31 PM
Yes
 
It doesn't even step into .then(function(bool){.. now
 
You can use it like any other object (return it, pass it as an argument, etc).
 
Why use resolve, reject in the promise arguments?
What are they used for?
Function object with two arguments resolve and reject. The first argument fulfills the promise, the second argument rejects it.
What..
 
What's special about a promise, is that you can call .then() on it, which, if you think about it in the original sense of callbacks, is like registering a callback to be called when the function ends (but not quite)
 
That's how to fulfill your promise.
 
8:33 PM
@Catgocat imagine this
 
Yeah .then and catch
 
That's when you are making a promise yourself, as opposed to get GETTING one from, say, a db library.
 
And then you have the Promise function that does the work and it completes when all async operations are done
 
function makeXHRRequest (method, url, done) {
  return new Promise(function (resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.open(method, url);
    xhr.onload = function () {
      resolve(xhr.response);
    };
    xhr.onerror = function () {
      reject(xhr.response);
    };
    xhr.send();
  });
}
 
Ohhh
resolve is the .then ?
 
8:33 PM
@Catgocat Hold on :P
 
Ok
 
The function above takes a few parameters and returns a promise.
The promise resolves when an XHR request is completed, or is rejected when the onerror handler is invoked.
 
it does
 
@Catgocat Ding
 
ok got it
 
8:34 PM
I had to double-take several times because of strict mode mentality
 
@Catgocat So now, with that promise object, you can do stuff
 
@SecondRikudo Yeah, and what are resolve and reject?
Where are they specified?
 
var XHRPromise = makeXHRRequest('get', 'http://google.com');

XHRPromise
    .then(function(responseText) {
        // response text is available here
    });
 
So the function(responseText) is the resolve function inside the promise right?
In that case.
 
8:36 PM
@l0oky you are failing it before the promise even had a shance to resolve.
look up testing asyncronous code
 
@Catgocat Not exactly.
 
@Catgocat It doesn't replace resolve in this case, no.
 
So where does it come from
The resolve
 
!!magic
 
(∩ ͡° ͜ʖ ͡°)⊃━☆゚. * ・ 。 ᵀᴴᴱ ᴳᴬᴹᴱ
 
8:37 PM
They're arguments
 
Where are those arguments passed?
 
They come from the Promise library, or are supplied by the JS engine if it's the built-in promises
 
new Promise(function (resolve, reject) {
    // ...
});
 
The resolve function is more of a marker, to tell the promise resolver that "This promise is now resolved! Please call all of the handlers passed with .then() now!"
Same with reject for rejection (and the .catch() method, or the less known second parameter to .then())
 
@Luggage If I remove .fail() It will succeed..
 
8:38 PM
Wait
 
Without going into that async code haha
 
Yup
 
Ok, and the reject is it for catch?
 
the function returns before it has a chance to ever happen.
 
@Catgocat Yes
 
8:39 PM
And that's it?
 
@Catgocat Conceptually, that's pretty much it. Yes.
 
resolve and reject are for moving the promise into the appropriate state; then and catch are for when this state change occurs
 
What does .then return ?
 
@Catgocat Another promise
 
The promise itself?
 
8:40 PM
No, a new one.
 
A copy of the other one?
 
.then() doesn't mutate a promise.
@Catgocat Nope.
Sec
 
So what If you want to have multiple callBacks for when the promise completes, you have to put all these in one .then?
 
function delayPromise(time) {
    return new Promise(function(resolve, reject) { setTimeout(resolve, time); });
}
 
yey got it to work
 
8:41 PM
This function will return a promise that returns after time milliseconds have passed
 
updating the code..
 
Yup. That's it, except that you aren' catching any errors.
so, it'll just hang if there is an error
 
Yeah how can I do that? :D
 
var one = delayPromise(1000);
var two = one.then(function() { return delayPromise(1000); };
var stillTwo = one.then(function() { return delayPromise(1000); };
 
@SecondRikudo When is a promise complete or pending?
 
8:43 PM
@Catgocat A promise has three states. Pending, resolved or rejected.
 
Resolve for the then, rejected for catch and pending when it hasnt done anything yet
?
 
Assuming asynchronousity, a promise always starts pending, and at some point in the future may change to resolve or rejected.
Handlers passed to .then() are all invoked when the promise state is resolved
even if it resolved before the call to .then() happened
.catch acts a little bit differently.
 
So what's the point of calling .then(..).then(..)
 
@l0oky docs.sequelizejs.com/en/latest/docs/models/… has exampke of .catch()
 
If the first one completed, the second one won't ever be called.
 
8:44 PM
@Catgocat Remember how I told you .then() returned a promise?
 
Yup
 
No, the second one 'fires' with the value of the first .then()
 
@Luggage sequelize docs has a broken search input.. Thanks!
 
makeXHR()
    .then(function(response) { return JSON.parse(response); })
    .then(function(jsonResponse) { return db.findAsync({id: response.id}); })
    .then(function(dbItem) { console.log(dbItem.name); });
Every call to .then() returns a new Promise immediately
makeXHR() returns a promise.
makeXHR()
    .then(function(response) { return JSON.parse(response); })
also returns a promise
A promise that resolves once JSON.parse finishes running (not entirely accurate since JSON.parse is synchronous, but let's go with it)
 
@l0oky .then() and .catch() aren't specific to sequelize, they are normal ways of using Promises. That's what the other conversation in here is also about.
 
8:47 PM
makeXHR()
    .then(function(response) { return JSON.parse(response); })
    .then(function(jsonResponse) { return db.findAsync({id: response.id}); })
Also returns a promise
 
nice.
 
A promise that resolves once a request has finished, JSON finished parsing, and database lookup on it has completed.
 
var p1 = new Promise(function(resolve, reject) {

	setTimeout(function() {
		resolve("hallow");
	}, 1000)
})

p1
	.then(function() { console.log('done') })
	.then(function() { console.log('done2'); })
 
I'd still prefer an actual monad
 
They return imediatelly
 
8:49 PM
@Catgocat No, they return one second later.
Both at the same time, yes. Because console.log doesn't take time to resolve.
 
So I have to manually return a Promise
 
@Catgocat Not really
In real code, you almost never need to use the new Promise constructor.
bluebird, for once, has Promise.promisify which takes a function that takes a callback, and returns a new function that does the same and returns a promise.
 
Can you give me a very short example (with setTimeout for async) in which you have a promise chain?
 
And its more badass version, Promise.promisifyAll which takes an object and adds promise methods on it for every callback-receiving method in it.
@Catgocat Sure
 
@Luggage Thank you very much. I am really nothing without a community like this :D
 
8:52 PM
function delayPromise(time) {
    return new Promise(function(resolve, reject) { setTimeout(resolve, time); });
}
do you understand how this function work? ^
 
Yes I do
delayPromise(5000).then(..)
 
delayPromise(1000)
    .then(function() { console.log('one'); })
    .then(function() { return delayPromise(1000); })
    .then(function() { console.log('two'); })
    .then(function() { return delayPromise(1000); })
    .then(function() { console.log('three'); });
 
Would it work the same way if you put the return delayPromises inside the functions that have console.log right
 
@Catgocat Indeed
.then(function() { console.log('one'); return delayPromise(1000); })
 
Interesting!
 
8:55 PM
.then() returns a promise regardless
If you return a value from it, it will resolve immediately (relative to its "parent" promise)
If you return a promise, the new promise will resolve when the returned promise resolves.
(And with the same resolve value)
 
Why and when would you use Promise.reject and resolve?
 
@Catgocat bluebird is smart about throw and Errors inside of promises
 
Those are helpers to just make a new promise that immiediate gets resolved like:
 
But the "correct" way to mark errors would be to return a promise that rejects with an Error object.
 
Promise.resolve('hello');
// same as:
new Promise(function(resolve, reject) { resolve('hello'); });
 
8:58 PM
It's the same.. but the second one doesn't solve it right away.
 
As for Promise.resolve() it allows you to easily start a promise chain from a direct value
 
The first does.
 
@Catgocat They are equivalent.
Both will resolve immediately.
 
@SecondRikudo Oh I see for example Promise.resolve(startChain).then(..).then(..)
 
@Catgocat Yes
 
8:59 PM
Thanks a lot @SecondRikudo you have my vote
 
@Catgocat You're welcome, and thanks :)
This is the best tutorial on the subject I've yet to find ^
 
I would like to leave you alone but I have a few questions though
 
@Catgocat Shoot
 
Does the promise run without any .then or .catch methods?
They are just a callBack right?
 
@Catgocat Yes, the promise still runs, and resolves/rejects even without you calling .then on it.
 
9:01 PM
And how can you access the [[PromiseStatus]]
 
Some libraries I have seen do NOT run until a .then() is called, but that's not a rule.
 
@Catgocat Bluebird offers the promise.settle() method
 
What is bluebird?
Because in my country it's a shopping mall
 
@Catgocat Bluebird is the best Promise library today.
!!tell Catgocat google bluebird promises
 
9:03 PM
Native promises only have .then() ( and .catch() ?). Bluebird has lots of other userful variations.
 
Does it use the Promise itself?
 
@Catgocat It uses a different implementation from native, which is actually faster than native.
(For most cases)
 
Nice nice.
 
So bluebird has .settle() which always resolves when promise settles (either resolves or rejects)
 
What would be a good use for using a promise library?
Games?
 
9:04 PM
It resolves with a PromiseReflection (or something like that) object, that allows you to inspect the status of the promise.
 
well, anything asynchronous. Database access, file access, ajax.
 
@Catgocat I have included bluebird in virtually all JavaScript projects, server and client side I've done since I've learned about Promises.
 
The resolve inside the Promise function, calls all the callBacks if there are multiple .then right?
 
Literally anything you can think of that you invoke now and happens in the future, is a Promise.
function ready() {
    return new Promise(function(resolve, reject) {
        window.onload = resolve;
    });
}
ready().then(function() {
    // window.onload fired! DOM is ready!
});
I already gave the example of XHR
 
var p = new Promise(function(res, rej) {
	res("done");
});

p.then(console.log);
p.then(console.log);
How would I do this if the Promise doesn't have to wait?
 
9:08 PM
@Catgocat Remember, .then() handlers are called when the promise is resolved, even if the promise resolved before you called .then()
 
@Catgocat What do you mean “wait”?
 
So even this works as you expect:
 
@SecondRikudo In that example, .then doesn't trigger it causes an error.
 
var p = new Promise(function(res, rej) {
	res("done");
});
setTimeout(function() { p.then(console.log); }, 1000);
 
@Catgocat An error about console.log being invoked indirectly?
 
9:09 PM
@Catgocat It causes an error because you can't pass console.log like that in the browser.
 
Pass console.log.bind(console).
 
Oh ok
Damn this
var p = new Promise(function(res, rej) {
	res("done");
});

p.then(console.log.bind(console));
p.then(console.log.bind(console));

2done
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
Perfect :)
And what is the PromiseValue for?
 
var p = new Promise(function(res, rej) {
	res("done");
});
setTimeout(function() { p.then(console.log.bind(console)); }, 1000);
Even this works ^ .then() is called at least one second after the promise resolves.
 
it'll be "done" once it's resolved, since that is what you are resolve()ing it with.
 
9:11 PM
@Luggage The promise resolved and the PromisValue is still undefined.
 
you showed it's state before it was resolved.
hence "pending" as the status.
 
@Catgocat Try p.then(function() { console.log(p); })
 
@Catgocat No it's not, read its status
 
Oh ye..
Only after ok
 
The function you pass the the promise constructor is guaranteed to be called asynchronously.
 
9:12 PM
Well, even if it had resolved, it’d still be undefined. console.log returns undefined.
 
var p = new Promise(function(res, rej) {
res("d2one", 2);
});

p.then(console.log.bind(console));
p.then(console.log.bind(console));
console.log(p)
Is the PromiseValue the first argument that is passed to the resolve function?
Because it shows d2one
// Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: "d2one"}
 
You can answer that question yourself
 
Well I don't see a practical reason for them to do that :p
 
How would you do it?
 
9:16 PM
I mean the [[PromiseValue]]
 
Remember that you can resolve with an array
 
What is that used for?
 
That's the whole purpose of the promise, to return a value at some point int he future.
 
That's an internal property, just useful for debugging.
 
though, you typically get that with .then(), not by inspecting it's state.
 
9:17 PM
You can't get a hold of it with js
(other than treating it like a promise, of course)
 
And how would you go to know the state of the Promise?
 
You don't
 
From an outter prespective.
 
you just use .then and .cathc. That's the sate.
You never have to say "are you done?" because you use .then to say "call me WHEN you are done"
 
Yeah just wondering
What next for ES6?
 
9:18 PM
nothing
es7 has async, though
 
@NickDugger And very bad streams, from what I gather
 
Tail recursion?
Is it implemented :ooooooooooooooooooo
 
@Zirak implementation or nature of the language?
 
@NickDugger I missed the funny
 
I'm not funny
 
9:20 PM
Precisely
 
Tail recursion finally
That stack won't overflow
 
What on earth is tail call optimization?
 
Recursion? Calling yourself at the end is called tail recursion. When that gets too deep, stack explodes. TCO (=tail call optimisation) makes sure it doesn't go boom.
 
In conclusion is recursion that doesn't overflow :p
It's much better really.
function factorial(n, acc = 1) {
    if (n <= 1) return acc;
    return factorial(n - 1, n * acc);
}

// Stack overflow in most implementations today,
// but safe on arbitrary inputs in ES6
console.log( factorial(1000000000) )
 
@Zirak oh
 
9:23 PM
@SomeKittens getting rid of unnecessary stack frames
 
λ babel-node es.js
Infinity
Worth the time
 
@KendallFrey So it detects that I'm recursing and doesn't create a whole new stack frame for every call?
 
It works kind of like this:
 
Well, not quite safe on arbitrary inputs. 1e16 - 1 === 1e16
 
// Tail recursion
function factorial(n, acc = 1) {
    if (n <= 1) return acc;
    return factorial(n - 1, n * acc);
}

// Normal recursion
function factorial(n) {
	if (n <= 1) return 1;
	return n * factorial(n - 1);
}
 
9:25 PM
when you call a function and return the value directly (i.e. return fun();) normally the computer goes "call this function, get it to return a value to me, and I'll return it to my caller." TCO changes that to "call this function, and get it to return a value to my caller directly."
 
@KendallFrey very helpful, thanks
 
so before the function is called, it removes the current function from the stack
 
@KendallFrey Your knowledge gives me orgasms
 
Your mother gives me orgasms
 
Haha I was just thinking of that
 
9:28 PM
So, all you need to prevent stack overflow is call recursive function on the last line?
 
But it was your mother instead
@Applejack Yes sir
 
@Applejack If your compiler has TCO enabled, yes
 
If you do 1 * recursive
You won't get TCO
 
not necessarily the last line, but the end of the function
 
Javascript chat here, who cares about the compilers :)
 
9:29 PM
so in a return works too
@Applejack you, apparently
 
// Tail recursion
function factorial(n, acc = 1) {
    if (n <= 1) return acc;
    return factorial(n - 1, n * acc);
}

// Normal recursion recursion
function factorial(n, acc = 1) {
    if (n <= 1) return acc;
    return 1 * factorial(n - 1, n * acc);
}
Notice the 1 * which doesn't really modify the output but makes the function non-tail recursion
 
recursion recursion #mindblown
@Catgocat better explain why
 
Because the computer processes that in a different way. It needs to have that 1 * before so the stack grows and grows
 
it thinks it needs to do math on the return value before returning it to the caller, so it won't optimize it
 
When it just has the returning function it calls that function and returns the value directly to the calleer, excluding unnecessary stackframes
Even if you add a + before the function, it doesnt do tail recursion.
// Normal recursion
function factorial(n, acc = 1) {
    if (n <= 1) return acc;
    return +factorial(n - 1, n * acc);
}
Stupid computer
*throws tomatoes*
 
9:34 PM
There's no reason TCO isn't possible there, it just takes a compiler smart enough to see it.
 
Even if you add a whitespace it won't do it.
Ok now I'm joking
 
@SecondRikudo (and anyone else) gist.github.com/SomeKittens/49222d46c0b3e1002fe8
@Zirak && @BenjaminGruenbaum if you're still around, review pl0x
 
Context:
Apr 13 at 11:55, by Second Rikudo
Canonical idea: Write a canonical question and answer about promisifying XHR for a 500 bounty from me. Ping me for details. (Preference to folks under 10k)
 
// lib/math.js
export function sum(x, y) {
  return x + y;
}
export var pi = 3.141593;
// app.js
import * as math from "lib/math";
alert("2π = " + math.sum(math.pi, math.pi));
 
9:44 PM
I mean my answer would be that and github.com/github/fetch
 
This is cool ^
import {lol} from "child.js";

console.log( lol(1, 2) )

// Cannot find module 'child.js'
It's in the same folder, any suggestions? ^
 
Might be ./child.js
 
Yup thanks
 
@minitech up to @SecondRikudo, as he's the one requesting
 
What is the correct antonym for 'resolved' (in the context of promises)? What do you call a promise that is not yet a promise? If that makes any kind of sense..
 
9:54 PM
pending promise
:p
 

« first day (1645 days earlier)      last day (3307 days later) »