diff --git a/Cakefile b/Cakefile index 0307b6bf..01fdd1e2 100644 --- a/Cakefile +++ b/Cakefile @@ -409,6 +409,17 @@ runTests = (CoffeeScript) -> description: description if description? source: fn.toString() if fn.toString? + global.supportsAsync = if global.testingBrowser + try + new Function('async () => {}')() + yes + catch + no + else + [major, minor, build] = process.versions.node.split('.').map (version) -> + parseInt version, 10 + major >= 8 or (major is 7 and minor >= 6) + helpers.extend global, require './test/support/helpers' # When all the tests have run, collect and print errors. @@ -428,6 +439,8 @@ runTests = (CoffeeScript) -> # Run every test in the `test` folder, recording failures. files = fs.readdirSync 'test' + unless global.supportsAsync # Except for async tests, if async isn’t supported. + files = files.filter (filename) -> filename isnt 'async.coffee' for file in files when helpers.isCoffee file literate = helpers.isLiterate file diff --git a/lib/coffeescript/repl.js b/lib/coffeescript/repl.js index 93019d6b..8b16d09b 100644 --- a/lib/coffeescript/repl.js +++ b/lib/coffeescript/repl.js @@ -26,7 +26,7 @@ } })(), historyMaxInputSize: 10240, - eval: async function(input, context, filename, cb) { + eval: function(input, context, filename, cb) { var Assign, Block, Call, Code, Literal, Value, ast, err, isAsync, js, referencedVars, result, token, tokens; // XXX: multiline hack. input = input.replace(/\uFF00/g, '\n'); @@ -71,10 +71,11 @@ result = runInContext(js, context, filename); // Await an async result, if necessary if (isAsync) { - result = (await result); - if (!sawSIGINT) { - cb(null, result); - } + result.then(function(resolvedResult) { + if (!sawSIGINT) { + return cb(null, resolvedResult); + } + }); return sawSIGINT = false; } else { return cb(null, result); diff --git a/package.json b/package.json index 16120faf..a6212a0d 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "version": "2.0.0-beta4", "license": "MIT", "engines": { - "node": ">=7.6.0" + "node": ">=6" }, "directories": { "lib": "./lib/coffeescript" diff --git a/src/repl.coffee b/src/repl.coffee index 7ac2cc1a..3193a3e3 100644 --- a/src/repl.coffee +++ b/src/repl.coffee @@ -44,9 +44,9 @@ replDefaults = result = runInContext js, context, filename # Await an async result, if necessary if isAsync - result = await result - cb null, result unless sawSIGINT - sawSIGINT = false + result.then (resolvedResult) -> + cb null, resolvedResult unless sawSIGINT + sawSIGINT = no else cb null, result catch err diff --git a/test/repl.coffee b/test/repl.coffee index c0ce1a12..f1a60915 100644 --- a/test/repl.coffee +++ b/test/repl.coffee @@ -115,6 +115,13 @@ testRepl "keeps running after runtime error", (input, output) -> input.emitLine 'a' eq 'undefined', output.lastWrite() +testRepl "#4604: wraps an async function", (input, output) -> + return unless global.supportsAsync + input.emitLine 'await new Promise (resolve) -> setTimeout (-> resolve 33), 10' + setTimeout -> + eq '33', output.lastWrite() + , 20 + process.on 'exit', -> try fs.unlinkSync historyFile