First steps in removing promises

333 views
Skip to first unread message

Rasmus Andersson

unread,
Feb 19, 2010, 8:26:20 PM2/19/10
to r...@tinyclouds.org, nod...@googlegroups.com
I've spend my Friday night starting to clean promises out of node (got
a life? what's that? :P).

Just passed all tests for the changes I've done to node.js -- I have
to say the code is now much cleaner just by these simple changes.

http://github.com/rsms/node/tree/callbacks

Basically, what's happening is this:

function someFunction(arg0, arg1) {
return someCall(arg0, arg1).addCallback(function(arg) {
// continue on success branch
}).addErrback(function(err) {
// continue on error branch
});
}

becomes

function someFunction(arg0, arg1, callback) {
return someCall(arg0, arg1, function(err, arg) {
if (err) // continue on error branch
else // continue on success branch
});
}

I've already ported the module loading code (and some of the fs
functions, naturally).

--
Rasmus Andersson

r...@tinyclouds.org

unread,
Feb 19, 2010, 8:31:43 PM2/19/10
to Rasmus Andersson, nod...@googlegroups.com
On Fri, Feb 19, 2010 at 5:26 PM, Rasmus Andersson <ras...@notion.se> wrote:
> I've spend my Friday night starting to clean promises out of node (got
> a life? what's that? :P).

Sorry, I've already done it. The one remaining piece is the multipart
library. I would like help with that.

Kris Kowal

unread,
Feb 19, 2010, 8:56:52 PM2/19/10
to nod...@googlegroups.com
On Fri, Feb 19, 2010 at 5:26 PM, Rasmus Andersson <ras...@notion.se> wrote:
> function someFunction(arg0, arg1, callback) {
>  return someCall(arg0, arg1, function(err, arg) {
>     if (err) // continue on error branch
>     else // continue on success branch
>  });
> }

What is the return value of "someCall"?

Kris Kowal

Rasmus Andersson

unread,
Feb 19, 2010, 9:19:20 PM2/19/10
to r...@tinyclouds.org, nod...@googlegroups.com

Yikes. So, 4 hours spend in vain :(

Have you deprecated *Sync methods?

IMHO there's no longer any need for them, as an absent callback would
indicate sync operation (as per the process.fs C++ implementation).

Rasmus Andersson

unread,
Feb 19, 2010, 9:24:47 PM2/19/10
to r...@tinyclouds.org, nod...@googlegroups.com
BTW, I've also taken the time to add sensible defaults to many of the
functions in the fs module. For instance, using umask for default
value to mkdir:

// truncate(fd, [mode=(0777^umask)], [callback])
exports.mkdir = function (path, mode, callback) {
if (typeof mode === 'function') {
callback = mode;
mode = undefined;
}
if (mode === undefined)
mode = 0777 ^ process.umask();
return process.fs.mkdir(path, mode, callback);
};

Rasmus Andersson

unread,
Feb 19, 2010, 9:30:10 PM2/19/10
to r...@tinyclouds.org, nod...@googlegroups.com
I also noticed a lot of stray calls to debug() in node.js, many of
which wasted many CPU cycles. By simply if-ing with debugLevel we can
avoid calling all that code just for nothing. e.g:

debug("looking for " + JSON.stringify(id) + " in " + JSON.stringify(paths));

become:

if (debugLevel > 0)
debug("looking for " + JSON.stringify(id) + " in " + JSON.stringify(paths));

--
Rasmus Andersson

Micheil Smith

unread,
Feb 20, 2010, 12:38:45 AM2/20/10
to nod...@googlegroups.com
Which branch / where is it. I'll need this to do further work on the filesystem module. I have more ideas on them as well, ping me on irc when you're about please.

- Micheil

> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>

Rasmus Andersson

unread,
Feb 20, 2010, 8:10:27 AM2/20/10
to nod...@googlegroups.com
On Sat, Feb 20, 2010 at 06:38, Micheil Smith <mic...@brandedcode.com> wrote:
> Which branch / where is it. I'll need this to do further work on the filesystem module. I have more ideas on them as well, ping me on irc when you're about please.

It's in ry/master

>
> - Micheil
>
> On 20/02/2010, at 12:31 PM, r...@tinyclouds.org wrote:
>
>> On Fri, Feb 19, 2010 at 5:26 PM, Rasmus Andersson <ras...@notion.se> wrote:
>>> I've spend my Friday night starting to clean promises out of node (got
>>> a life? what's that? :P).
>>
>> Sorry, I've already done it. The one remaining piece is the multipart
>> library. I would like help with that.
>>
>> --
>> You received this message because you are subscribed to the Google Groups "nodejs" group.
>> To post to this group, send email to nod...@googlegroups.com.
>> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
>> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>
>

--
Rasmus Andersson

Isaac Z. Schlueter

unread,
Feb 21, 2010, 4:54:37 PM2/21/10
to nodejs
On Feb 19, 5:31 pm, r...@tinyclouds.org wrote:
> Sorry, I've already done it. The one remaining piece is the multipart
> library. I would like help with that.

What would you like done with the multipart lib? We talked about
making it a bit more SAXy, and removing the stream/http coupling,
which I'd definitely be willing to do.

--i

Ryan Dahl

unread,
Feb 21, 2010, 6:40:55 PM2/21/10
to nod...@googlegroups.com

I already stripped out the promises. As a first step, it'd be great to
decouple it from HTTP so that the interface was

var p = new multipart.Parser();
req.addListener('data', function (d) { p.execute(d); }
req.addListener('end', function () { p.finish(); }

Brian Hammond

unread,
Feb 22, 2010, 12:22:15 AM2/22/10
to nodejs
Sorry but I'm playing catch-up again. Why are Promises being retired?

Ryan Dahl

unread,
Feb 22, 2010, 12:31:54 AM2/22/10
to nod...@googlegroups.com
On Sun, Feb 21, 2010 at 9:22 PM, Brian Hammond <br...@fictorial.com> wrote:
> Sorry but I'm playing catch-up again. Why are Promises being retired?

Because many people (myself included) only want a low-level interface
to file system operations that does not necessitate creating an
object, while many other people want something like promises but
different in one way or another. So instead of promises we'll use last
argument callbacks and consign the task of building better abstraction
layers to user libraries.

See http://groups.google.com/group/nodejs/msg/0c483b891c56fea2

Elijah Insua

unread,
Feb 22, 2010, 1:09:24 AM2/22/10
to nod...@googlegroups.com
Keep node small and concise! hack the planet!

-- Elijah

Reply all
Reply to author
Forward
0 new messages