Skip to content

Instantly share code, notes, and snippets.

@glennblock
Created April 15, 2012 20:29
Show Gist options
  • Save glennblock/2394695 to your computer and use it in GitHub Desktop.
Save glennblock/2394695 to your computer and use it in GitHub Desktop.
async exits with streamline
//this does not work because exists does not accept a standard callback
function createRequiredFilesIfNotPresent(_) {
var baseFolder = process.cwd();
var exists = path.exists(baseFolder + "/server.js");
log.info(exists);
}
//using a wrapper function however works. Log entries added for sequence.
function exists(p, callback) {
path.exists(p, function(e) {
callback(null, e);
});
}
function createRequiredFilesIfNotPresent(_) {
var baseFolder = process.cwd();
log.info("check exists start");
var p1 = exists(baseFolder + "/server.js", _);
log.info("exists:" + p1);
log.info("check exists complete");
}
@bjouhier
Copy link

Hi Glenn,

Yes, the vast majority of node callback based core APIs take a standard callback. path.exist is an exception and the little exists wrapper that you wrote is the way to deal with it.

There is also another gotcha with core APIs: the "futures" feature does not work directly with them, it only works with the functions that you have written in streamline.

One remark: you should swap lines 20 and 21. Also you can directly write:

log info("exists: " + exists(baseFolder + "/server.js", _));

@glennblock
Copy link
Author

glennblock commented Apr 16, 2012 via email

@bjouhier
Copy link

Yes, I could provide a small library with it, or at least document it. I want to write a FAQ but I keep getting dragged into other things, like blog articles.

I don't want to put it directly into streamline's runtime though because I want to keep streamline at the language level. It runs in the browser too.

@glennblock
Copy link
Author

glennblock commented Apr 17, 2012 via email

@bjouhier
Copy link

Yes.

I have bundled a helper for streams because streams have an event API that does not work too well with streamline. So I have introduced a callback API on top of it that works much better with streamline. I did it because this is a non trivial thing and it makes a big difference in usability.

But I did not take care of all the corner cases of the node API. I could introduce a nodeutils module with other wrappers. I'll do it when I get time.

I track all this through the GitHub issues system. So if you want to enter the suggestion there please do it. Otherwise, I'll do it.

Bruno

@glennblock
Copy link
Author

glennblock commented Apr 17, 2012 via email

@bjouhier
Copy link

bjouhier commented Apr 17, 2012 via email

@glennblock
Copy link
Author

glennblock commented Apr 17, 2012 via email

@bjouhier
Copy link

Sure. My skype is offline most of the time but I'll turn it on. But that won't be before tomorrow as I'm travelling today.

@bjouhier
Copy link

Forgot. my skype id is bjouhier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment