then
The following should work? Finally expects a block statement, so need to wrap it in { }...
var info, processed; try { info = JSON.parse(json); } catch (exception) { console.log("Information JSON malformed."); } finally { try { processed = process(info); } catch (exception) { console.log("Error processing information."); }}
The idea is to not do the processing if the JSON parsing fails.
From: Cryptic Swarm [mailto:crypticswarm at gmail.com] Sent: Friday, November 9, 2012 11:55 To: Domenic Denicola Cc: es-discuss at mozilla.org Subject: Re: then
The following should work? Finally expects a block statement, so need to wrap it in { }...
var info, processed; try { info = JSON.parse(json); } catch (exception) { console.log("Information JSON malformed."); } finally { try { processed = process(info); } catch (exception) { console.log("Error processing information."); }} On Fri, Nov 9, 2012 at 1:46 PM, Domenic Denicola <domenic at domenicdenicola.com<mailto:domenic at domenicdenicola.com>> wrote:
The recent promise discussion prompted me to recall the following musing/proposal from Kris Kowal:
kriskowal/q/wiki/On-Exceptions
In short, there's a common code pattern that you can do with promises that you can't do with synchronous code. The proposal is for a language extension then
that would allow synchronous code like this:
var info, processed; try { info = JSON.parse(json); } catch (exception) { console.log("Information JSON malformed."); } then { processed = process(info); } catch (exception) { console.log("Error processing information."); }
Since seeing this, I've run into a lot of situations where a then
would come in handy. I wanted to see if anyone else thought this was a great idea, and if it has any chance as an ES7-level proposal.
On Fri, Nov 9, 2012 at 11:57 AM, Domenic Denicola < domenic at domenicdenicola.com> wrote:
The idea is to not do the processing if the JSON parsing fails.****
From: Cryptic Swarm [mailto:crypticswarm at gmail.com] Sent: Friday, November 9, 2012 11:55 To: Domenic Denicola Cc: es-discuss at mozilla.org Subject: Re: then****
The following should work? Finally expects a block statement, so need to wrap it in { }...****
var info, processed; try { info = JSON.parse(json); } catch (exception) { console.log("Information JSON malformed."); } finally { try { processed = process(info); } catch (exception) { console.log("Error processing information."); }}****
On Fri, Nov 9, 2012 at 1:46 PM, Domenic Denicola < domenic at domenicdenicola.com> wrote:****
The recent promise discussion prompted me to recall the following musing/proposal from Kris Kowal:
kriskowal/q/wiki/On-Exceptions
In short, there's a common code pattern that you can do with promises that you can't do with synchronous code. The proposal is for a language extension
then
that would allow synchronous code like this:var info, processed; try { info = JSON.parse(json); } catch (exception) { console.log("Information JSON malformed."); } then { processed = process(info); } catch (exception) { console.log("Error processing information."); }
Since seeing this, I've run into a lot of situations where a
then
would come in handy. I wanted to see if anyone else thought this was a great idea, and if it has any chance as an ES7-level proposal.
es-discuss mailing list es-discuss at mozilla.org, mail.mozilla.org/listinfo/es-discuss****
es-discuss mailing list es-discuss at mozilla.org, mail.mozilla.org/listinfo/es-discuss
I believe you want that to desugar into:
var info, processed; try { info = JSON.parse(json); try { processed = process(info); } catch (exception) { console.log("Error processing information."); } } catch (exception) { console.log("Information JSON malformed."); }
... which is the synchronous equivalent of callbacks marching ever rightward, and it is ugly and hard to read.
Like deeply-nested if statements, it's more readable if you put it into the context of a function and follow an "early exit" coding style rather than a "only one exit" coding style:
var info, processed; try { info = JSON.parse(json); } catch (exception) { console.log("Information JSON malformed."); return; } try { processed = process(info); } catch (exception) { console.log("Error processing information."); return; }
This still breaks up the "normal" flow of the function, but I don't see this as significantly less readable than the proposed try/catch/then.
| | The idea is to not do the processing if the JSON parsing fails. |
Then, why not just nest your try's? For readability reasons?
var info, processed;
try {
info = JSON.parse(json);
try {
processed = process(info);
} catch(ex) {
...
}
} catch (ex) {
...
}
If it's the case, you could maybe use a Promise-like approach? With a bit of coding, you could probably even get something like
val info, processed;
trySequence(=> {
info = JSON.parse(json);
}).catch(ex => {
...
}).then(=> {
processed = process(info);
}).catch(ex => {
...
}).;
The recent promise discussion prompted me to recall the following musing/proposal from Kris Kowal:
kriskowal/q/wiki/On-Exceptions
In short, there's a common code pattern that you can do with promises that you can't do with synchronous code. The proposal is for a language extension
then
that would allow synchronous code like this:var info, processed; try { info = JSON.parse(json); } catch (exception) { console.log("Information JSON malformed."); } then { processed = process(info); } catch (exception) { console.log("Error processing information."); }
Since seeing this, I've run into a lot of situations where a
then
would come in handy. I wanted to see if anyone else thought this was a great idea, and if it has any chance as an ES7-level proposal.