Reputation: 8334
in C# you have await statements. to handle async requests. I am using typscript because I find it really hard to work with javascript
Now when using typescript i dont know what is the rigth way to handle requests when posts have been processed.
what is the best way to handle that.
Upvotes: 2
Views: 2473
Reputation: 251172
Both the async
keyword (1.7) and the backwards-compilation for ECMAScript 6 (2.0) are planned for TypeScript. In the meantime, both Q and RSVP follow the Promises/A+ standard - and both have typings available from Definitely Typed.
There is a proposal for introducing the async
keyword to TypeScript.
This feature is scheduled to be part of TypeScript 1.7 (no guarantees).
The really good news is that there will, in fact, be a backwards compile to ES5 (this is scheduled for TypeScript 2.0). This is not the only backwards compile that TypeScript does, so don't take too much notice of people who say that TypeScript doesn't add anything to JavaScript except types - there are polyfills for many ECMAScript 6 features either already available or planned, for example (but not limited to):
import * from 'module';
let
There are even occasional ECMAScript 7 features landing on the roadmap now, in TypeScript 1.7.
Upvotes: 2
Reputation: 19738
TypeScript does not provide new functionality to JavaScript, it "merely" introduces typing. As such, you'll need to look at how different JavaScript libraries handle async requests,choose your likings and reference the corresponding definition file.
For the first part, I'm going to assume you are using jQuery
(as your question is tagged likewise). In jQuery, async requests are handled by promises. I suggest reading the docs and looking online for other tutorials on how to use them. I will provide a TypeScript example below.
After you've chosen which JavaScript library to use, you'll need a TypeScript definition file (*.d.ts
). Basically this is equivalent to a C header file. A definition file will tell the TypeScript compiler there exist scopes, variables and methods without having to provide a TypeScript implementation. In case of the jQuery defintion file, it will tell the compiler there's a scope named $
as well as all variables and methods defined on that scope. A socially maintained repository of defintion files can be found here. It also includes documentation on how to reference the files in your project.
Lastly, you'll need to make use of promises in your TypeScript code, example code below.
$.post("http://www.hateverurl.dom", options).done(() => {
// the POST request has finished succesfully when this method is invoked.
})
Upvotes: 4