Last active
August 29, 2015 14:23
-
-
Save ochafik/14be0372c46fbfb77310 to your computer and use it in GitHub Desktop.
Simple example of async/await transform for Dart's dev_compiler (see https://github.com/dart-lang/dev_compiler/issues/221)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run with dart: | |
// dart async_example.dart | |
import 'dart:async'; | |
f(g) async { | |
try { | |
final value = await g(); | |
return 'Result: ' + (value + 1); | |
} catch (e) { | |
if (e == 'rethrow me') throw e; | |
return 'Caught: ' + e; | |
} | |
} | |
printResult(title, g) => f(g).then( | |
(v) => print('[$title] Success Result: $v'), | |
onError: (e) => print('[$title] Error Result: $e')); | |
main() { | |
printResult('resolved value', () => new Future.value(10)); | |
printResult('rejected value', () => new Future.error('error1')); | |
printResult('caught error', () { throw 'catch me'; }); | |
printResult('rethrown error', () { throw 'rethrow me'; }); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run with io.js: | |
// iojs --harmony_arrow_functions async_example.js | |
"use strict"; | |
if (typeof dart == "undefined") var dart = {}; | |
dart.Await = function(promise, callback) { | |
this.promise = promise; | |
this.callback = callback; | |
}; | |
dart.Future = { | |
value: v => new Promise((resolve, reject) => resolve(v)), | |
error: e => new Promise((resolve, reject) => reject(e)) | |
}; | |
dart.consumeAwaits = function(iterator) { | |
try { | |
let result = iterator.next(); | |
if (result.value instanceof dart.Await) { | |
let await = result.value; | |
var nextPromise = await.promise.then( | |
v => await.callback(() => v), | |
e => await.callback(() => { throw e })); | |
return result.done ? nextPromise : nextPromise.then(() => dart.consumeAwaits(iterator)); | |
} else { | |
if (!result.done) { | |
throw new Error('Invalid state: not expecting non-dart.Await values before the iterator is done.'); | |
} | |
return dart.Future.value(result.value); | |
} | |
} catch (e) { | |
return dart.Future.error(e); | |
} | |
}; | |
var f = (g) => dart.consumeAwaits((function*() { | |
var callback$1; | |
try { | |
var value = ((yield new dart.Await(g(), cb => callback$1 = cb)) || callback$1()); | |
return 'Result: ' + (value + 1); | |
} catch (e) { | |
if (e == 'rethrow me') throw e; | |
return 'Caught: ' + e; | |
} | |
})()); | |
var printResult = (title, g) => f(g).then( | |
v => console.log(`[${title}] Success Result: ${v}`), | |
e => console.log(`[${title}] Error Result: ${e}`)); | |
printResult('resolved value', () => dart.Future.value(10)); | |
printResult('rejected value', () => dart.Future.error('error1')); | |
printResult('caught error', () => { throw 'catch me' }); | |
printResult('rethrown error', () => { throw 'rethrow me' }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment