2

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.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
matthy
  • 8,144
  • 10
  • 38
  • 47
  • 2
    Answer: Use promises. – SLaks Oct 06 '13 at 16:52
  • Typescript doesn't add functionality to the web browser platform. You'll need to write code to handle it using your favorite "async" platform. If you're using jQuery (as you've tagged it), then just use jQuery.Ajax for the async requests. It's quite simple. – WiredPrairie Oct 06 '13 at 18:57
  • 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. – Fenton Nov 12 '15 at 23:06
  • The Promise is the best object for handle all async requests. [fix async requests with Promise object](https://stackoverflow.com/a/47719849/7487135) – Iman Bahrampour Dec 08 '17 at 18:07

2 Answers2

4

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.
})
thomaux
  • 19,133
  • 10
  • 76
  • 103
  • 2
    It would be great though if TypeScript implemented `async/await` support. After all, it's just syntactic sugar around state machine objects, and JavaScript has all gears for this. In fact, [it's being discussed](http://typescript.codeplex.com/discussions/438647). – noseratio Oct 11 '13 at 07:28
2

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.

In Depth

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
  • generators
  • async / promises
  • destructuring

There are even occasional ECMAScript 7 features landing on the roadmap now, in TypeScript 1.7.

Fenton
  • 241,084
  • 71
  • 387
  • 401