1

I have a piece of code that returns a JQuery promise:

const jqProm = server.downloadAsync();

I wish to use it inside async function. I was hoping I could create something like C# TaskCompletionSource, return dummy Task (Promise) and as a handler for jqProm resolve/reject set the status of dummy Task (Promise).

How to do that? I can find only Promise constructor that takes in resolver and rejector actions - but there is no thing like PromiseCompletionSource.

user3284063
  • 665
  • 5
  • 20
  • in the world of javascript Promise/A+ ... what is a `PromiseCompletionSource` – Jaromanda X Apr 06 '17 at 12:14
  • C#-think will only serve to confuse. jQuery 3+, do nothing, promises are already Promises/A+ compliant. jQuery <3 or if you need sugar methods of native/other promise, then `let promise = Promise.resolve(jQueryPromise)`. – Roamer-1888 Apr 06 '17 at 13:17
  • The `resolve` and `reject` *resolver functions* **are** the JS equivalent of a `PromiseCompletionSource`. But I'm not sure what you need this for? – Bergi May 02 '17 at 10:40

2 Answers2

3

jQuery promises are thenables, which means that you can use them with native promises just fine. They will implicitly get Promise.resolved. All you need to write is

await server.downloadAsync();
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
2

I've personally run into that matter today. This should do the trick.

Converting a jQuery promise into an Angular promise:

var angularPromise = $q.when(jQueryPromise); // eg: $q.when($.get(...));

Converting a jQuery promise to a native or Bluebird promise:

var promise = Promise.resolve(jQueryPromise); // eg: Promise.resolve($.get(..));
Community
  • 1
  • 1
Kyle_jumpen
  • 111
  • 2
  • 9