I'm refactoring some promise code to using async
/await
, and it works great except for one thing.
Something that a Promise
lets you do is sort of "inject" the callback wherever you'd like. Ex:
public DoSomethingAsync() {
return new Promise<void>(done => {
// do some stuff
done();
});
}
Or even maybe the callback is wrapped up in another callback:
public DoSomethingAsync() {
return new Promise<void>(done => {
const somethingDeferred = GetSomethingDeferred();
somethingDeferred.onsuccess = () => {
// some success code
done();
};
});
}
There is a similar problem with the "reject" handler.
Is there a way to refactor these to async
/await
? Or are they stuck wrapped up in a new Promise...
block?
I think it's possible that the first example can be reduced all the way to simply:
public async DoSomethingAsync() {
// do some stuff
}
Because the callback will automatically be called afterwards anyway. But the second example seems much more problematic.