This example demonstrates the use of async/await using TypeScript v1.9-dev, Node.js v6 and Visual Studio Code v1.0;
$ node lib/await.js
1
2
3
Error: throw error to showcase source map support.
at /async_await_typescript/lib/await.ts:21:9
at undefined.next (native)
at fulfilled (/async_await_typescript/lib/await.js:4:58)
Manually compile using gulp. To run tsc directly, use tsc -p .
- Download Visual Studio Code 1.0 or later
node -vEnsure node is v6 or laternpm install .Install deps defined in package.json
File ▸ Auto SaveEnable auto save.- Gulp tasks are auto detected
and made available in the task list.
On a Mac
Shift + Command + P ▸ Run Task ▸ Press Enterthen select Watch. - Watch automatically compiles TypeScript according to the gulpfile rules.
.
├── .vscode
│ ├── launch.json
│ └── settings.json
├── lib
│ ├── await.js
│ └── await.js.map
├── node_modules
├── ts
│ └── await.ts
├── .gitignore
├── gulpfile.js
├── jsconfig.json
├── package.json
├── readme.md
├── tsconfig.json
.vscode/settings.json- Settings for Visual Studio Code.Code ▸ Preferences ▸ Workspace Settingsto change..vscode/launch.json- Launch Configuration that enables debugging in Visual Studio code.lib/await.js- Output from TypeScript.libis a mix between ES6 and ES5. This code runs on node v6 or better.lib/await.js.map- Source maps allows tracing failures back to line numbers in the original typescript files.node_modules- The dependencies defined inpackage.jsonts/await.ts- TypeScript source files which are automatically transpiled on save by typescript..gitignore- Standard git ignore file.gulpfile.js- Gulp build system used to setup source maps, babel, and typescript.jsconfig.json- Visual Studio Code config file that defines the version of JavaScript used by the editor.package.json- Standard npm package config that defines dependencies and other metadata useful when publishing to npm.readme.md- The document you're currently reading.tsconfig.json- The typescript compiler options.
- Node is still missing many ES6 features.
node --v8-options | grep "in progress"
Node lacks support for ES6 modules because v8 hasn't implemented them
As a result module type commonjs is used in tsconfig.json.
- typescript Transpiles ES 7 to ES 6
- node.js Runs ES 6/5 hybrid
TypeScript offers granular targeting via the lib compiler option specified in tsconfig.
Visual Studio Code supports debugging TypeScript with source map support.
- Thanks to @ivogabe for blogging about using typescript with babel
- Thanks to @Deathspike for posting an async example
